问题描述
我有一个父对象调用页面,有对象的列表,名为控制:
公共类页
{
名单< CustomControl>控制{获取;集;}
}
该CustomControl类具有以下确定指标:
公共类CustomControl
{
字符串名称{获取;集;}
字符串值{获取;集;}
}
例如说Page类有两个CustomControls A和B是否有可能通知自定义控件B计算的自定义控件的属性值更改,以便它可以改变它的一些属性。
我在想,现在执行的CustomControl类INotifyPropertyChanged的事件我怎么通知CustomControl的实例时同一个类的其他实例有其修改了一些属性。
公共类CustomControl:INotifyPropertyChanged的
{
私人字符串_name;
公共字符串名称
{
{返回_name; }
组
{
如果(_name ==值)回报;
_name =价值;
的PropertyChanged(这一点,新PropertyChangedEventArgs(名称));
}
}
私人字符串_value;
公共字符串值
{
得到{_value; }
组
{
如果(_value ==值)回报;
_value =值;
的PropertyChanged(这一点,新PropertyChangedEventArgs(值));
}
}
公共事件PropertyChangedEventHandler的PropertyChanged =委托{};
内部虚拟无效OnSiblingInPagePropertyChanged(对象发件人,PropertyChangedEventArgs参数)
{
}
}
公共类CustomControlObservableColletion:的ObservableCollection< CustomControl>
{
//必需的,因为,在默认情况下,这是不可能找出哪些项目
//已被清除时,CollectionChanged事件.Clear()调用之后被激发。
保护覆盖无效ClearItems()
{
的foreach(在Items.ToList VAR项目())
删除(项目);
}
}
公共类页
{
公众的IList< CustomControl>控制{获得;私定; }
公共页面()
{
无功控制=新CustomControlObservableColletion();
controls.CollectionChanged + = OnCollectionChanged;
控制=控制;
}
私人无效OnCollectionChanged(对象发件人,NotifyCollectionChangedEventArgs E)
{
开关(e.Action)
{
案例NotifyCollectionChangedAction.Add:
RegisterControls(e.NewItems);
打破;
案例NotifyCollectionChangedAction.Replace:
RegisterControls(e.NewItems);
DeRegisterControls(e.OldItems);
打破;
案例NotifyCollectionChangedAction.Remove:
DeRegisterControls(e.OldItems);
打破;
}
}
私人无效RegisterControls(IList的控制)
{
的foreach(对照CustomControl控制)
control.PropertyChanged + = OnControlPropertyChanged;
}
私人无效DeRegisterControls(IList的控制)
{
的foreach(对照CustomControl控制)
control.PropertyChanged - = OnControlPropertyChanged;
}
私人无效OnControlPropertyChanged(对象发件人,PropertyChangedEventArgs E)
{
的foreach(在Controls.Where功控制(C =>!C =发送者))
control.OnSiblingInPagePropertyChanged(发件人,E);
}
}
I have a parent object called Page that has a List of objects called Control:
public class Page
{
List<CustomControl> controls {get;set;}
}
The CustomControl class has the following defintion:
public class CustomControl
{
string Name {get;set;}
string Value {get;set;}
}
Say for instance the Page class has two CustomControls A and B. Is it possible to Notify Custom Control B when the property Value of Custom Control A changes so that it can change some of its properties.
I was thinking of implementing the INotifyPropertyChanged event on the CustomControl class now how do I notify an instance of the CustomControl when another instance of the same class has some property of its Modified.
public class CustomControl : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (_name == value) return;
_name = value;
PropertyChanged(this, new PropertyChangedEventArgs("Name"));
}
}
private string _value;
public string Value
{
get { return _value; }
set
{
if (_value == value) return;
_value = value;
PropertyChanged(this, new PropertyChangedEventArgs("Value"));
}
}
public event PropertyChangedEventHandler PropertyChanged = delegate { };
internal virtual void OnSiblingInPagePropertyChanged(object sender, PropertyChangedEventArgs args)
{
}
}
public class CustomControlObservableColletion : ObservableCollection<CustomControl>
{
// Required because, by default, it is not possible to find out which items
// have been cleared when the CollectionChanged event is fired after a .Clear() call.
protected override void ClearItems()
{
foreach (var item in Items.ToList())
Remove(item);
}
}
public class Page
{
public IList<CustomControl> Controls { get; private set; }
public Page()
{
var controls = new CustomControlObservableColletion();
controls.CollectionChanged += OnCollectionChanged;
Controls = controls;
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
switch (e.Action)
{
case NotifyCollectionChangedAction.Add:
RegisterControls(e.NewItems);
break;
case NotifyCollectionChangedAction.Replace:
RegisterControls(e.NewItems);
DeRegisterControls(e.OldItems);
break;
case NotifyCollectionChangedAction.Remove:
DeRegisterControls(e.OldItems);
break;
}
}
private void RegisterControls(IList controls)
{
foreach (CustomControl control in controls)
control.PropertyChanged += OnControlPropertyChanged;
}
private void DeRegisterControls(IList controls)
{
foreach (CustomControl control in controls)
control.PropertyChanged -= OnControlPropertyChanged;
}
private void OnControlPropertyChanged(object sender, PropertyChangedEventArgs e)
{
foreach (var control in Controls.Where(c => c != sender))
control.OnSiblingInPagePropertyChanged(sender, e);
}
}
这篇关于通知一个对象时的另一个对象的属性发生更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!