C# 如何跟踪“类型”字段值的变化;“可观察到的收集”是什么;?

C# 如何跟踪“类型”字段值的变化;“可观察到的收集”是什么;?,c#,wpf,data-binding,C#,Wpf,Data Binding,如何跟踪“ObservableCollection”类型字段值的变化 我写了一个例子,其中我计算了具有绑定的字段更改的数量。在本例中,我得到了“Str”字段的变化量,但无法得到“Arr”字段的变化量 MyClass.cs using System; using System.Collections.ObjectModel; using System.ComponentModel; namespace BindingArrayItem { public class MyClass : I

如何跟踪“ObservableCollection”类型字段值的变化

我写了一个例子,其中我计算了具有绑定的字段更改的数量。在本例中,我得到了“Str”字段的变化量,但无法得到“Arr”字段的变化量

MyClass.cs

using System;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace BindingArrayItem
{
    public class MyClass : INotifyPropertyChanged
    {
        private int changeCount;
        public int ChangeCount
        {
            get
            {
                return changeCount;
            }
            set
            {
                changeCount = value;
                OnPropertyChanged("ChangeCount");
            }
        }

        private String str;
        public String  Str
        {
            get
            {
                return str;
            }
            set
            {
                str = value;
                OnPropertyChanged("Str");
                AddCount(); // In this place the function is triggered 
            }
        }

        private ObservableCollection<String> arr = new ObservableCollection<String>();
        public ObservableCollection<String> Arr
        {
            get
            {
                return arr;
            }
            set
            {
                arr = value;
                AddCount(); // At this point, the function does not work (and no exception) !!!
            }
        }

        public MyClass()
        {
            ChangeCount = 0;

            Arr = new ObservableCollection<String>();
            Arr.Add("111");
            Arr.Add("222");

            Str = "333";
        }

        private void AddCount()
        {
            ChangeCount++;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string info)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(info));
            }
        }
    }
}
使用系统;
使用System.Collections.ObjectModel;
使用系统组件模型;
名称空间绑定ArrayItem
{
公共类MyClass:INotifyPropertyChanged
{
私有int变更计数;
公共整数更改计数
{
得到
{
返回更改计数;
}
设置
{
changeCount=值;
OnPropertyChanged(“ChangeCount”);
}
}
私有字符串str;
公共字符串Str
{
得到
{
返回str;
}
设置
{
str=值;
不动产变更(“Str”);
AddCount();//在此处触发函数
}
}
私有ObservableCollection arr=新ObservableCollection();
公共可观测收集
{
得到
{
返回arr;
}
设置
{
arr=值;
AddCount();//此时,函数不工作(没有异常)!!!
}
}
公共MyClass()
{
ChangeCount=0;
Arr=新的可观测集合();
协议增补(“111”);
协议增补(“222”);
Str=“333”;
}
私有void AddCount()
{
ChangeCount++;
}
公共事件属性更改事件处理程序属性更改;
私有void OnPropertyChanged(字符串信息)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(处理程序!=null)
{
处理程序(此,新属性ChangedEventArgs(信息));
}
}
}
}
MainWindow.xaml.cs

using System.Windows;

namespace BindingArrayItem
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private MyClass myClass = null;

        public MainWindow()
        {
            InitializeComponent();

            myClass = new MyClass();
            this.label1.DataContext = myClass;
            this.label2.DataContext = myClass;
            this.label3.DataContext = myClass;
            this.label4.DataContext = myClass;
        }

        private void button_Click(object sender, RoutedEventArgs e)
        {
            myClass.Str = "333";
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            myClass.Arr[1] = "333";
        }
    }
}
使用System.Windows;
名称空间绑定ArrayItem
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
私有MyClass MyClass=null;
公共主窗口()
{
初始化组件();
myClass=新的myClass();
this.label1.DataContext=myClass;
this.label2.DataContext=myClass;
this.label3.DataContext=myClass;
this.label4.DataContext=myClass;
}
私有无效按钮\u单击(对象发送者,路由目标e)
{
myClass.Str=“333”;
}
私有无效按钮1\u单击(对象发送者,路由目标)
{
myClass.Arr[1]=“333”;
}
}
}
MainWindow.xaml

<Window x:Class="BindingArrayItem.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:BindingArrayItem"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Label Name="label1" Content="{Binding Arr[0]}" HorizontalAlignment="Left" Margin="40,40,0,0" VerticalAlignment="Top"/>
        <Label Name="label2" Content="{Binding Arr[1]}" HorizontalAlignment="Left" Margin="40,80,0,0" VerticalAlignment="Top"/>
        <Label Name="label3" Content="{Binding Str}" HorizontalAlignment="Left" Margin="40,120,0,0" VerticalAlignment="Top"/>
        <Label Name="label4" Content="{Binding ChangeCount}" HorizontalAlignment="Left" Margin="40,160,0,0" VerticalAlignment="Top"/>
        <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="78,227,0,0" VerticalAlignment="Top" Width="75" Click="button_Click"/>
        <Button x:Name="button1" Content="Button" HorizontalAlignment="Left" Margin="207,227,0,0" VerticalAlignment="Top" Width="75" Click="button1_Click"/>
    </Grid>
</Window>

仅当通过为属性指定值时,才会调用
Arr
属性设置器

Arr = someValue;
当您访问集合对象时,例如通过

Arr[i] = someValue;
但是,您可以订阅集合的CollectionChanged事件:

private ObservableCollection<string> arr = new ObservableCollection<string>();

public ObservableCollection<string> Arr
{
    get => arr;
    set
    {
        if (arr != null) arr.CollectionChanged -= ArrCollectionChanged;
        arr = value;
        if (arr != null) arr.CollectionChanged += ArrCollectionChanged;
    }
}

private void ArrCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
    AddCount();
}
private observetecollection arr=new observetecollection();
公共可观测收集
{
get=>arr;
设置
{
如果(arr!=null)arr.CollectionChanged-=ArrCollectionChanged;
arr=值;
如果(arr!=null)arr.CollectionChanged+=arrclectionchanged;
}
}
私有无效ArrCollectionChanged(对象发送方,NotifyCollectionChangedEventArgs e)
{
AddCount();
}