简体   繁体   中英

Binding to List<myType> WPF

I have myType1 with one dependency property string Text . I crated myType2 that contains dependency property ObservableCollection<myType1> Items . I also have graphical representation of Items . When i press button, it sets myType1.Text to null . When Item.Text from Items is null I want to delete this item. 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. What am i doing wrong. Thank you!

Update

According to documentation ObservableCollection doesn't raise CollectionChanged event when item's property is changed. I solved my problem by inheritance from 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. I solved my problem by inheritance from 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.
  2. In CollectionChanged handler method i subscribe on PropertyChanged of each item.
  3. In PropertyChanged handler method i write needed behavior. Guess it will be helpful for somebody.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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