Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从代码隐藏中的可观察集合中选择的项_C# - Fatal编程技术网

C# 从代码隐藏中的可观察集合中选择的项

C# 从代码隐藏中的可观察集合中选择的项,c#,C#,这个问题确实很常见,但我想了解,如果可能的话,找到解决办法。我们如何倾听来自某个项目的事件,该项目是从代码隐藏中的可观察集合中选择的。 我写了一个例子: 第一类人员: public class Personne : INotifyPropertyChanged{ private int _age = 10; public int Age{ get { return _age; } set { _age = value; OnPropertyChange

这个问题确实很常见,但我想了解,如果可能的话,找到解决办法。我们如何倾听来自某个项目的事件,该项目是从代码隐藏中的可观察集合中选择的。 我写了一个例子:

第一类人员:

public class Personne : INotifyPropertyChanged{
    private int _age = 10;
    public int Age{
       get { return _age; }
       set { _age = value; OnPropertyChanged("Age");  }

       }

    private string _name="";
    public string Name
    {
       get { return _name;  }
       set { _name = value; OnPropertyChanged("Name"); }

    }
    public Personne() { }
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}
现在进入主课堂:

 static void Main(string[] args)
    {
        int indice = 0;
        ObservableCollection<Personne> _collection = new ObservableCollection<Personne>();
        _collection.CollectionChanged += _collection_CollectionChanged;
        _collection.Add(new Personne());            
        Personne tmp = _collection[indice];
        indice++;
        tmp.PropertyChanged += Tmp_personne_PropertyChanged;
        tmp.Age = indice;
        _collection.Add(new Personne());
        tmp = _collection[indice];
        indice++;
        tmp.Age = indice;




    }

    private static void _collection_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
       int a =1;
        a++;

    }

    private static void Tmp_personne_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        int a = 1;
        a++;
    }
} 

触发事件,但第二个没有。为什么

您的代码示例不太清楚,问题描述也不清楚。但是如果我理解正确,您会问为什么在
tmp.Age=indice的第二个示例中没有引发
PropertyChanged
事件,而它在第一个中引发。如果这是正确的,那么简单的答案就是你还没有订阅。如果您在
OnPropertyChanged()
方法中设置一个断点并逐步执行代码,您可以自行确定。在这种情况下,您会发现
handler
null
,这是一个明确的信号,表明您尚未订阅该事件tmp
对象后的code>语句,将引发事件,就像在第一个示例中一样。'事实上,所选项目每次都可以动态更改。因此,我只想听听selecteditem egal,他指的是哪些项目。事实上,直接订阅
PropertyChanged
事件可能不是正确的方法。但是你的问题中没有足够的上下文让你回到正确的方向。你的代码示例不太清楚,你的问题描述也不清楚。但是如果我理解正确,您会问为什么在
tmp.Age=indice的第二个示例中没有引发
PropertyChanged
事件,而它在第一个中引发。如果这是正确的,那么简单的答案就是你还没有订阅。如果您在
OnPropertyChanged()
方法中设置一个断点并逐步执行代码,您可以自行确定。在这种情况下,您会发现
handler
null
,这是一个明确的信号,表明您尚未订阅该事件tmp
对象后的code>语句,将引发事件,就像在第一个示例中一样。'事实上,所选项目每次都可以动态更改。因此,我只想听听selecteditem egal,他指的是哪些项目。事实上,直接订阅
PropertyChanged
事件可能不是正确的方法。但是在你的问题中没有足够的上下文让你回到正确的方向。
 Personne tmp = _collection[indice];
    indice++;
    tmp.PropertyChanged += Tmp_personne_PropertyChanged;
    tmp.Age = indice;