【问题标题】:Raising event when Property in ObservableCollection changes当 ObservableCollection 中的属性发生变化时引发事件
【发布时间】:2018-07-30 10:03:49
【问题描述】:

我想在 DataGrid 中的属性发生更改时引发事件以检查它是否有效,将其保存回我的源文件等。

背景信息: 我有一个绑定到可观察集合的 DataGrid。 此时我已经成功地将我的 Observable Collection 绑定到视图,但是我还没有设法在属性更改时引发事件。 两种方式绑定也可以工作,因为我可以通过调试观察到集合的变化。 我通过 BindableBase(Prism) 继承了 INotifyPropertyChanged。

public ObservableCollection<CfgData> Cfg
{
    get { return _cfg; }
    set { SetProperty(ref _cfg, value); }
}
private ObservableCollection<CfgData> _cfg;

CfgData 包含 4 个属性:

public class CfgData
{
    public string Handle { get; set; }
    public string Address { get; set; }
    public string Value { get; set; }
    public string Description { get; set; }

    public CfgData(string handle, string address, string value)
    {
        this.Handle = handle;
        this.Address = address;
        this.Value = value;
    }

    public CfgData(string handle, string address, string value, string description)
    {
        this.Handle = handle;
        this.Address = address;
        this.Value = value;
        this.Description = description;
    }
}

我正在使用从 csv 读取的值填充我的 Observable 集合。文件

public ObservableCollection<CfgData> LoadCfg(string cfgPath)
{
var cfg = new ObservableCollection<CfgData>();
try
{
    using (var reader = new StreamReader(cfgPath))
    {
        while (!reader.EndOfStream)
        {
            var line = reader.ReadLine();
            var values = line.Split(';');

            if (values.Length == 3)
            {
                cfg.Add(new CfgData(values[0], values[1], values[2]));
            }
            else if (values.Length == 4)
            {
                cfg.Add(new CfgData(values[0], values[1], values[2], values[3]));
            }
        }
    }
}
catch (Exception x)
{
    log.Debug(x);
}
return cfg;
}

我的 XAML

<DataGrid Name="cfgDataGrid" Margin="10,10,109,168.676" ItemsSource="{Binding Cfg, Mode=TwoWay}" AutoGenerateColumns="False">
  <DataGrid.Columns>
    <DataGridTextColumn Header="Handle" Binding="{Binding Path=Handle}" Width="auto" IsReadOnly="True" />
    <DataGridTextColumn Header="Address" Binding="{Binding Path=Address}" Width="auto" IsReadOnly="True" />
    <DataGridTextColumn Header="Value" Binding="{Binding Path=Value}" Width="auto" IsReadOnly="False" />
    <DataGridTextColumn Header="Description" Binding="{Binding Path=Description}" Width="auto" IsReadOnly="True" />
  </DataGrid.Columns>
</DataGrid>

问题 2 路绑定更新我的视图模型中的集合。但是我想在保存之前验证输入。我还希望能够添加一些功能,例如在验证编辑时调用方法。因此,我尝试使用几种事件处理方式,例如

this.Cfg.CollectionChanged += new NotifyCollectionChangedEventHandler(Cfg_OnCollectionChanged);

this.Cfg.CollectionChanged += Cfg_OnCollectionChanged;

但是,当我更改数据网格时,那些从未调用过函数。

问题 如何创建在属性更改时调用的事件处理程序?我必须保存整个数据集还是只保存更改的数据行/属性?

【问题讨论】:

    标签: c# wpf events datagrid prism


    【解决方案1】:

    因为ObservableCollection 没有观察到他的物品。它将引发插入、删除项目或重置集合的事件,而不是对其项目的修改。

    因此,您必须实现ObservableCollection,它同样观察他的项目。在我的项目中使用的这段代码是在 SO 上找到的,但我无法弄清楚帖子的原文。当我们将新项目添加到集合中时,它会为其添加一个 INotifyPropertyChanged 事件。

        public class ItemsChangeObservableCollection<T> :
               System.Collections.ObjectModel.ObservableCollection<T> where T : INotifyPropertyChanged
        {
            protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    RegisterPropertyChanged(e.NewItems);
                }
                else if (e.Action == NotifyCollectionChangedAction.Remove)
                {
                    UnRegisterPropertyChanged(e.OldItems);
                }
                else if (e.Action == NotifyCollectionChangedAction.Replace)
                {
                    UnRegisterPropertyChanged(e.OldItems);
                    RegisterPropertyChanged(e.NewItems);
                }
    
                base.OnCollectionChanged(e);
            }
    
            protected override void ClearItems()
            {
                UnRegisterPropertyChanged(this);
                base.ClearItems();
            }
    
            private void RegisterPropertyChanged(IList items)
            {
                foreach (INotifyPropertyChanged item in items)
                {
                    if (item != null)
                    {
                        item.PropertyChanged += new PropertyChangedEventHandler(item_PropertyChanged);
                    }
                }
            }
    
            private void UnRegisterPropertyChanged(IList items)
            {
                foreach (INotifyPropertyChanged item in items)
                {
                    if (item != null)
                    {
                        item.PropertyChanged -= new PropertyChangedEventHandler(item_PropertyChanged);
                    }
                }
            }
    
            private void item_PropertyChanged(object sender, PropertyChangedEventArgs e)
            {
                //launch an event Reset with name of property changed
                base.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
            }
        }
    }
    

    接下来,你的模型

    private ItemsChangeObservableCollection<CfgData> _xx = new ItemsChangeObservableCollection<CfgData>();
    public ItemsChangeObservableCollection<CfgData> xx 
    {
        get { return _xx ;}
        set { _xx = value; }
    }
    

    最后但同样重要的是,您的模型必须实现 INotifyPropertyChanged

    public class CfgData: INotifyPropertyChanged
    {
    
    }
    

    【讨论】:

    • 感谢您的快速回复。我必须在 ViewModel 中使用 ObservableCollection&lt;CfgData&gt; 而不是 ItemsChangeObservableCollection&lt;CfgData&gt; 才能使以前的 DataBinding 正常工作。您的解决方案确实触发了 ItemsChangeObservableCollection 类中的 item_PropertyChanged 事件。作为后续问题:我应该如何调用包含旧/新值比较逻辑的方法?我是否从 item_PropertyChanged 方法委托给我的视图模型中的方法?
    • ItemsChangeObservableCollectionObservableCollection。 DataBinding 必须与它一起使用。
    • 谢谢,在修改了我的LoadCfg 方法后,我注意到我创建了一个ItemsChangeObservableCollection&lt;CfgData&gt; 的新实例。在从我的文件中添加新值之前删除该行并清除集合启用了正确的数据绑定。我不确定这是否相关,但我还将属性的 XAML 从 'Binding="{Binding Path=Value}"` 更改为 'Binding="{Binding Value}"`。
    • 很高兴您的问题解决了。
    猜你喜欢
    • 2011-01-15
    • 1970-01-01
    • 1970-01-01
    • 2014-02-22
    • 2020-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-09
    相关资源
    最近更新 更多