简体   繁体   English

可观察的集合项属性已更改

[英]Observable collection item property changed

I have a column class which uses view model base to implement INotifyPropertyChanged (lazy I know): 我有一个列类,它使用视图模型库来实现INotifyPropertyChanged (我知道懒):

public class Column : ViewModelBase
{
    public string ColumnName { get; set; }
    public bool Anonymize { get; set; }
}

And then a list of columns: 然后列表列表:

public class Columns : ObservableCollection<Column>
{
}

In my view model I have a property columns and I am binding that to a combo box with a checkbox and textblock: 在我的视图模型中,我有一个属性列,我将其绑定到带有复选框和文本块的组合框:

private Columns _tableColumns;
public Columns TableColumns
{
  get
  {
    return _tableColumns;
  }
  set
  {
    _tableColumns = value;
    OnPropertyChanged("TableColumns");
  }
}
  <ComboBox Name="cbColumns" ItemsSource="{Binding TableColumns}">
    <ComboBox.ItemTemplate>
      <DataTemplate>
        <StackPanel Orientation="Horizontal">
          <CheckBox  IsChecked="{Binding Anonymize, Mode=TwoWay}" />
          <TextBlock Text="{Binding ColumnName}"/>
        </StackPanel>
      </DataTemplate>
    </ComboBox.ItemTemplate>
  </ComboBox>

When I change the Anonymize property through the checkbox on an item, how do make the Columns property change in the view model to reflect this? 当我通过项目上的复选框更改Anonymize属性时,如何使视图模型中的Columns属性更改以反映这一点?

Your Column class needs to implement INotifyPropertyChanged (which you say it does). 你的Column类需要实现INotifyPropertyChanged (你说它确实如此)。 You also need to raise that event it when the value of Anonymize changes (which you don't). Anonymize的值发生变化时(您不这样做),您还需要引发该事件。

If you want to change the Anonymize property only from the UI, you are done. 如果您只想从UI更改Anonymize属性,那么您就完成了。 If you'd like to see the changes(from the backend) on the UI, you have to implement the INotifyPropertyChanged interface in the Column class. 如果您想在UI上看到更改(来自后端),则必须在Column类中实现INotifyPropertyChanged接口。

public class Column : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    public string columnName;
    public bool anonymize;

    public string ColumnName
    {
        get { return columnName; }
        set
        {
            columnName = value; RaiseOnPropertyChanged("ColumnName");
        }
    }
    public bool Anonymize
    {
        get { return anonymize; }
        set { anonymize = value; RaiseOnPropertyChanged("Anonymize"); }
    }

    public void RaiseOnPropertyChanged(string propertyName)
    {
        var eh = PropertyChanged;
        if (eh != null)
            eh(this, new PropertyChangedEventArgs(propertyName));
    }
}

When the Anonymize state changes it will need to notify the view model that it needs to modify the collection of columns. Anonymize状态更改时,它将需要通知视图模型它需要修改列集合。 The way I've solved this before is to add a CheckChanged event to the Column class that is raised when Anonymize . 我之前解决这个问题的方法是将一个CheckChanged事件添加到Anonymize时引发的Column类。 The the view model subscribes to the event after it creates the Column object but it is added to the Columns collection. 视图模型在创建Column对象后订阅该事件,但它被添加到Columns集合中。 When CheckChanged is raised the view model adds/removes the item from the Columns collection. 引发CheckChanged时,视图模型会添加/删除Columns集合中的项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM