简体   繁体   English

如何使用INotifyPropertyChanged更新派生的属性

[英]How to update the derived properties using INotifyPropertyChanged

I have a few properties in a bindingList for a XtratreeList(DevExress) where a child node needs to show a parentnode'e property. 我在XtratreeList(DevExress)的bindingList中有一些属性,其中子节点需要显示parentnode'e属性。 I have the following code. 我有以下代码。

public abstract class ClassBase : INotifyPropertyChanged
{
    protected static int initialId = 0;

    private int id;
    private int parentID;
    private string productName;
    private string productType;
    private string colorProductType;

    private void RaisePropertyChanged(string propertyName)
    {
        if ( PropertyChanged != null )
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public int ID
    {
        get { return id; }
        set
        {
            if ( id == value )
                return;

            id = value;
            RaisePropertyChanged("ID");
        }
    }

    public int ParentID
    {
        get { return parentID; }
        set
        {
            if ( parentID == value )
                return;

            parentID = value;
            RaisePropertyChanged("ParentID");
        }
    }

    public string ProductName
    {
        get { return productName; }
        set
        {
            if ( productName == value )
                return;

            productName = value;
            RaisePropertyChanged("ProductName");
        }
    }

    public string ProductType
    {
        get { return productType; }
        set
        {
            if ( productType == value )
                return;

            productType = value;
            RaisePropertyChanged("ProductType");
            RaisePropertyChanged("ColorProductType");
        }
    }

    public string ColorProductType
    {
        get { return colorProductType ; }
        set
        {
            if (colorProductType == value)
                return;

            colorProductType = value;
            RaisePropertyChanged("ColorProductType");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}`

My requirement is to have the ColorProductType property changed when the ProductType property changes, basically ProductType is a parent node property and ColorProductType - child's. 我的要求是在ProductType属性更改时更改ColorProductType属性,基本上ProductType是父节点属性,而ColorProductType是子节点属性。 So on changing the parent's property the child's need to be changed. 因此,在更改父母的财产时,需要更改孩子的财产。 I have both these properties bound to 2 text boxes. 我将这两个属性都绑定到2个文本框。 So changing the parent prop should change both textboxes, but the vice versa is not true. 因此,更改父属性应同时更改两个文本框,但反之亦然。 RaisePropertyChanged("ColorProductType"); within the parent is not working, colorproducttype is null, what is the issue here? 在父级中不起作用, colorproducttype为null,这是什么问题?

RaisePropertyChanged does not actually update the property. RaisePropertyChanged实际上并不更新该属性。 It simply signals the PropertyChanged event. 它只是发出PropertyChanged事件的信号。 Something somewhere must subscribe to it and update the other property accordingly. 某处必须订阅它并相应地更新其他属性。 Something like this: 像这样:

public abstract class ClassBase : INotifyPropertyChanged
{
    private string productType;
    private string colorProductType;

    public ClassBase()
    {
        this.PropertyChanged += HandlePropertyChanged;
    }

    private void HandlePropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        if(e.PropertyName == "ProductType")
        {
            // update ColorProductType here
        }
    }

    private void RaisePropertyChanged(string propertyName)
    {
        if ( PropertyChanged != null )
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

    public string ProductType
    {
        get { return productType; }
        set
        {
            if ( productType == value )
                return;

            productType = value;
            RaisePropertyChanged("ProductType");
        }
    }

    public string ColorProductType
    {
        get { return colorProductType ; }
        set
        {
            if (colorProductType == value)
                return;

            colorProductType = value;
            RaisePropertyChanged("ColorProductType");
        }
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion
}

Naturally, this is complete overkill. 自然,这完全是矫over过正。 You can update ColorProductType when ProductType is updated and let the PropertyChanged event and databinding handle the textbox update: 您可以在更新ProductType时更新ColorProductType ,并让PropertyChanged事件和数据绑定处理文本框更新:

public string ProductType
{
    get { return productType; }
    set
    {
        if ( productType == value )
            return;

        productType = value;

        // update ColorProductType here

        RaisePropertyChanged("ProductType");
    }
}

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

相关问题 INotifyPropertyChanged 和不同对象上的派生属性 - INotifyPropertyChanged And derived properties on different objects 如何为派生类实现INotifyPropertyChanged? - How to implement INotifyPropertyChanged for derived classes? 如何为聚合属性实现 INotifyPropertyChanged - How to implement INotifyPropertyChanged for aggregate properties 如何在不使用INotifyPropertyChanged的情况下更新视图和视图模型 - How to Update View and View Model without using INotifyPropertyChanged 使用INotifyPropertyChanged更新ObservableCollection项属性 - Updating ObservableCollection Item properties using INotifyPropertyChanged 派生类上的INotifyPropertyChanged - INotifyPropertyChanged on a derived class 如何为c#属性访问方法? (特别是如果该属性是一个集合,并且使用INotifyPropertyChanged接口) - How are methods accessed for c# properties? (Specifically if the property is a collection and using the INotifyPropertyChanged interface) 如果仅更改属性的元素,则INotifyPropertyChanged不会更新属性 - INotifyPropertyChanged won't update a property if only the properties' elements changed 如何使用INotifyPropertyChanged更新数据绑定的ListBox的FontFamily和Foreground - How can I update FontFamily and Foreground of a data bound ListBox using INotifyPropertyChanged 使用 INotifyPropertyChanged - using of INotifyPropertyChanged
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM