简体   繁体   English

ObservableCollection,OnPropertyChanged未触发

[英]ObservableCollection, OnPropertyChanged not firing

I'm beginner and I tried to search everywhere for a similar problem, this question has been asked many times but I couldn't find any solution. 我是初学者,我尝试在任何地方搜索类似的问题,这个问题已被问过很多次,但是我找不到任何解决方案。

I implemented INotifyPropertyChanged like this (it is working correctly): 我像这样实现了INotifyPropertyChanged (它工作正常):

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            MessageBox.Show("property changed");
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

My observablecollection look like this : 我的observablecollection看起来像这样:

    ObservableCollection<bool>  Test = new ObservableCollection<bool>(new[] { false, false, false, false, false, false });

    private ObservableCollection<bool> _Collection;
    public ObservableCollection<bool> Collection
    {
        get { return _Collection = Test; }
        set { _Collection = value; OnPropertyChanged("Collection"); }
    }

This collection is bound to togglebutton's property contained in usercontrols : 此集合绑定到usercontrols中包含的togglebutton的属性:

VidFlipX="{Binding DataContext.Collection[1], ElementName=cmix, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

Data changed correctly while using them but if I set the collection in an other way, OnPropertyChanged is not firing and so my togglebuttons are not updated. 但数据变化,而正确使用他们,如果我在其他的方式设置的收集, OnPropertyChanged不点火,因此我的ToggleButtons不更新。

Can't find out why... 找不到原因...

With changing a item of your collection, you are not changing your collection. 通过更改收藏夹中的项目,您不会更改收藏夹。 So you are not calling the setter of your collection. 因此,您不会调用集合的二传手。 To update the item on your Ui, your items in your collection have to implement the INotifyPropertyChanged like described here . 要更新你的UI项目,您的收藏在你的项目必须实现INotifyPropertyChanged像描述在这里

A solution could be like this: 一个解决方案可能是这样的:

ObservableCollection<ClassA> Test { get; set; }

With your class: 与您的班级:

class ClassA : INotifyPropertyChanged
{
    private bool _isEnabled;
    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                OnPropertyChanged("IsEnabled");
            }
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

You must not call 你一定不要打电话

return _Collection = Test;

in the getter of your Collection property, because this renders the setter (and hence the whole INotifyPropertyChanged implementation) useless. 在您的Collection属性的getter中,因为这使setter(以及整个INotifyPropertyChanged实现)变得无用。 The value passed to the setter is never used anywhere. value传递到设置从未在任何地方使用。

When you need to initialize your property with a predefined collection, you could do it like this: 当您需要使用预定义的集合初始化属性时,可以这样做:

private ObservableCollection<bool> _Collection
    = new ObservableCollection<bool>(new[] { false, false, false, false, false, false });

public ObservableCollection<bool> Collection
{
    get { return _Collection; }
    set { _Collection = value; OnPropertyChanged("Collection"); }
}

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

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