MVVM WPF列表框条目不更新

MVVM WPF列表框条目不更新

问题描述:

我最近开始学习MVVM模式,并创建了一个简单的应用程序来测试几件事情。MVVM WPF列表框条目不更新

我有一个简单的查看与:项目

  • ListBox中持有的ObservableCollection
  • 删除按钮
  • 新建按钮
  • 文本框的项目说明
  • 文本框的项目值

除了事实是,如果我正在更新项目描述ListBox项不更新。我读了一些关于这个的文章,所以我认为它与CollectionChanged没有被调用有关。我尝试了一些可能的解决方案来解决这个问题,但是他们都没有成功所以也许我的方法总是有些问题。

希望有人能帮助我解决这个问题。

型号/ Item.cs

internal class Item : INotifyPropertyChanged 
{ 

    #region Fields 
    private string value; 
    private string description; 
    #endregion 

    #region Constructors 
    public Item() 
    { 
    } 

    public Item(string value, string description) { 
     this.description = description; 
     this.value = value; 
    } 
    #endregion 

    public String Value 
    { 
     get 
     { 
      return value; 
     } 
     set 
     { 
      this.value = value; 
      OnPropertyChanged("Value"); 
     } 
    } 

    public String Description 
    { 
     get 
     { 
      return description; 
     } 
     set 
     { 
      description = value; 
      OnPropertyChanged("Description"); 
     } 
    } 

    #region Overrides 
    public override string ToString() 
    { 
     return description; 
    } 
    #endregion String Override 

    #region INotifyPropertyChanged Members 
    public event PropertyChangedEventHandler PropertyChanged; 

    private void OnPropertyChanged(string propertyName) 
    { 
     PropertyChangedEventHandler handler = PropertyChanged; 

     if (handler != null) 
     { 
      handler(this, new PropertyChangedEventArgs(propertyName)); 
     } 

    } 
    #endregion 

} 

视图模型/ MainViewModel.cs

... 

private ObservableCollection<Item> items; 
private Item selectedItem; 

public ObservableCollection<Item> Items { 
    get 
    { 
     return items; 
    } 
    set 
    { 
     items = value; 
     OnPropertyChanged("Items"); 
    } 
} 
public Item SelectedItem { 
    get 
    { 
     return selectedItem; 
    } 
    set 
    { 
     selectedItem = value; 
     OnPropertyChanged("SelectedItem"); 
    } 
} 

... 

查看/ MainWindow.xaml

... 

<Button Content="New" Command="{Binding NewCommand}" /> 
<Button Content="Delete" Command="{Binding DeleteCommand}" /> 
<ListBox x:Name="lbxItems" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" /> 
<TextBox Text="{Binding SelectedItem.Description}" /> 
<TextBox Text="{Binding SelectedItem.Value}" /> 

... 
+0

您同时使用'SelectedRange'和'SelectedItem'的列表框/文本框绑定。这是打算? – Dirk

+0

你能否提供你的Item类实现? – sszarek

+1

当Description属性中的值更新时,是否调用OnPropertyChanged(“Description”)? – Bells

本的ItemTemplate它应该工作

<ListBox Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" ItemsSource="{Binding Items}" SelectedItem="{Binding SelectedItem}" > 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Horizontal"> 
        <TextBlock Text="{Binding Path=Value}" Margin="0,0,10,0/> 
        <TextBlock Text="{Binding Path=Description}" /> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
+0

它工作!谢谢! – systrance

你如何生成模型类PropertyChangedEvent?

试试这个:

internal class Range : INotifyPropertyChanged 
{ 
    private string _value; 

    public string Value 
    { 
     get { return _value; } 
     set 
     { 
      if (_value == value) return; 
      _value = value; 
      OnPropertyChanged("Value"); 
     } 
    } 
    //Do same for the Description property 
    //Do not forgot implement INotifyPropertyChanged interface here 
} 

我希望这可以帮助您

哦!你的更新后很清楚。您不使用任何数据模板作为列表框项目,因此WPF会为没有DT的显示项目调用ToString()方法。但是当你更新任何属性时,WPF并不知道这个,因为对象不会改变。 使用Datatemplate或尝试使用空字符串调用OnPropertyChanged。

+0

感谢您的提示,但我已经实施了INotifyPropertyChanged接口。我更新了我的问题并提供了Item类的完整实现。 – systrance