开发者

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. 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("ColorProdu开发者_如何转开发ctType");
        }
    }

    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. So on changing the parent's property the child's need to be changed. I have both these properties bound to 2 text boxes. 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?


RaisePropertyChanged does not actually update the property. It simply signals the PropertyChanged event. 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. You can update ColorProductType when ProductType is updated and let the PropertyChanged event and databinding handle the textbox update:

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

        productType = value;

        // update ColorProductType here

        RaisePropertyChanged("ProductType");
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜