简体   繁体   English

绑定到列表 <myType> WPF

[英]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.TextItems为空我想删除这个项目。 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;
    }
}
  1. In every constructor I subscribe on CollectionChanged event. 在每个构造函数中,我都订阅CollectionChanged事件。
  2. In CollectionChanged handler method i subscribe on PropertyChanged of each item. CollectionChanged处理程序方法中,我订阅每个项目的PropertyChanged
  3. In PropertyChanged handler method i write needed behavior. PropertyChanged处理程序方法中,我编写所需的行为。 Guess it will be helpful for somebody. 猜猜这对某人会有所帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM