[英]Binding to List<myType> WPF
I have myType1
with one dependency property string Text
. 我有
myType1
带有依赖项属性string Text
myType1
。 I crated myType2
that contains dependency property ObservableCollection<myType1> Items
. 我
myType2
了包含依赖项属性ObservableCollection<myType1> Items
。 I also have graphical representation of Items
. 我也有
Items
图形表示。 When i press button, it sets myType1.Text
to null
. 当我按下按钮时,它将
myType1.Text
设置为null
。 When Item.Text
from Items
is null I want to delete this item. 当
Item.Text
从Items
为空我想删除这个项目。 I try to do this via ` 我尝试通过`
private static void PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
ObservableCollection<StringDP> ocdp = e.NewValue as ObservableCollection<StringDP>;
foreach (var sdp in ocdp)
{
if (sdp == null)
{
ocdp.Remove(sdp);
}
}
dependencyObject.SetValue(e.Property, ocdp);
}
but it's not raises when Item.Text is setted to null. 但是当Item.Text设置为null时,它不会引发。 What am i doing wrong.
我究竟做错了什么。 Thank you!
谢谢!
Update 更新资料
According to documentation ObservableCollection
doesn't raise CollectionChanged
event when item's property is changed. 根据文档,当项目的属性更改时,
ObservableCollection
不会引发CollectionChanged
事件。 I solved my problem by inheritance from ObservableCollection
. 我通过继承
ObservableCollection
解决了我的问题。
`public class ElObservableCollection<T>: ObservableCollection<T> where T: INotifyPropertyChanged
{
public ElObservableCollection(): base()
{
CollectionChanged += OnCollectionChanged;
}
public virtual void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var item in Items)
{
item.PropertyChanged += OnItemChanged;
}
}
if (e.OldItems != null)
{
foreach (var item in Items)
{
item.PropertyChanged -= OnItemChanged;
}
}
}
private void OnItemChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "TextProperty" && sender is StringDP)
{
StringDP sdp = sender as StringDP;
if (sdp.Text == null)
{
this.Remove((T) sender);
}
}
}
public ElObservableCollection(List<T> list)
: base(list)
{
CollectionChanged += OnCollectionChanged;
}
public ElObservableCollection(IEnumerable<T> collection)
: base(collection)
{
CollectionChanged += OnCollectionChanged;
}
}`
在myType1内部创建一个引用myType2的属性,这样,如果myType1内部的text属性设置为null,则可以将其自身删除。
According to documentation ObservableCollection
doesn't raise CollectionChanged
event when item's property is changed. 根据文档,当项目的属性更改时,
ObservableCollection
不会引发CollectionChanged
事件。 I solved my problem by inheritance from ObservableCollection
. 我通过继承
ObservableCollection
解决了我的问题。
public class ElObservableCollection<T>: ObservableCollection<T> where T: INotifyPropertyChanged
{
public ElObservableCollection(): base()
{
CollectionChanged += OnCollectionChanged;
}
public virtual void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (e.NewItems != null)
{
foreach (var item in Items)
{
item.PropertyChanged += OnItemChanged;
}
}
if (e.OldItems != null)
{
foreach (var item in Items)
{
item.PropertyChanged -= OnItemChanged;
}
}
}
private void OnItemChanged(object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == "TextProperty" && sender is StringDP)
{
StringDP sdp = sender as StringDP;
if (sdp.Text == null)
{
this.Remove((T) sender);
}
}
}
public ElObservableCollection(List<T> list)
: base(list)
{
CollectionChanged += OnCollectionChanged;
}
public ElObservableCollection(IEnumerable<T> collection)
: base(collection)
{
CollectionChanged += OnCollectionChanged;
}
}
CollectionChanged
event. CollectionChanged
事件。 CollectionChanged
handler method i subscribe on PropertyChanged
of each item. CollectionChanged
处理程序方法中,我订阅每个项目的PropertyChanged
。 PropertyChanged
handler method i write needed behavior. PropertyChanged
处理程序方法中,我编写所需的行为。 Guess it will be helpful for somebody.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.