[英]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.