当 ObservableCollection 中的属性更改时引发事件

问题描述 投票:0回答:1

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

背景信息: 我有一个绑定到可观察集合的 DataGrid。 此时,我已成功将可观察集合绑定到视图,但是我还没有设法在属性更改时引发事件。 双向绑定也可以工作,因为我可以通过调试观察集合的更改。 我通过 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 读取的值填充我的可观察集合。文件

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个回答
2
投票

因为

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
{

}
© www.soinside.com 2019 - 2024. All rights reserved.