2015-04-23 101 views
1

我最近開始學習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

回答

2

本的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

0

你如何生成模型類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