manav inder
manav inder

Reputation: 3601

Dependency Property Update Mechanism

I have a custom control which is having a dependency property defined in it and my control implements INotifyPropertyChanged interface.

Dependency Property is Collection of Custom Objects.

Scenario 1

DP is of type List, whatever change I make in the list, nothing updated in MainUI, because I believe WPF does not understand adding and removing objects in list. it understands completely new references so to achieve this, whenever I want to update my list on control I use

MyProperty=new List();

In this approach, my DP callback fires everytime but eventArgs.NewValue always remains zero(it updated the list on UI correctly) but because I need to write some logic in property changed callback based on e.NewItems.Count, in this case that didn't work. Please tell me why e.NewItems does not work.

Scenario 2

DP is of type ObservableCollection, so as my collection property in view model against which I am binding my DP. in this case my property change callbacks does not fire at all, because I never use "new" keyword again after initialzing the property first time. UI updates but property change still not fires. So my logic in property change call back does not gets executed.

How should I make any of them or both of them working.

Upvotes: 1

Views: 282

Answers (2)

Peter Hansen
Peter Hansen

Reputation: 8907

I would use the ObservableCollection approach, and subscribe to it's CollectionChanged event.
That way you will get notified whenever the collection has been changed.

But the other approach should work as well. When you set the regular list to a new instance, the PropertyChangedCallback will be fired for the dependency property, and by examining the DependencyPropertyChangedEventArgs object you can get the new value.

XAML:

<StackPanel>
    <Button Content="Add to observablecollection" Click="click1" />
    <Button Content="Set list to new instance" Click="click2" />
</StackPanel>

Code-behind:

public partial class Window1 : Window
{
    public ObservableCollection<string> Strings { get; set; }

    public List<string> StringsList
    {
        get { return (List<string>)GetValue(StringsListProperty); }
        set { SetValue(StringsListProperty, value); }
    }

    public static readonly DependencyProperty StringsListProperty =
        DependencyProperty.Register("StringsList", typeof(List<string>), typeof(Window), new PropertyMetadata(null, StringsListPropertyChanged));

    public Window1()
    {
        InitializeComponent();

        Strings = new ObservableCollection<string>();
        Strings.CollectionChanged += strings_CollectionChanged;

        StringsList = new List<string> { "Test1", "Test2", "Test3", "Test4" };
    }

    void strings_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
    {
        //Fires everytime the observablecollection has an item added/removed etc.
        MessageBox.Show(string.Format("ObservableCollection has changed! Count is now {0}", this.Strings.Count.ToString()));

        if (this.Strings.Count == 10)
            Console.WriteLine("Collection contains 10 strings!!");
    }

    private static void StringsListPropertyChanged(DependencyObject e, DependencyPropertyChangedEventArgs args)
    {
        var newCount = ((List<string>)args.NewValue).Count.ToString();
        MessageBox.Show(string.Format("Dependency property has changed! Count is now {0}", newCount));
    }

    private void click1(object sender, RoutedEventArgs e)
    {
        this.Strings.Add("Test1");
    }

    private void click2(object sender, RoutedEventArgs e)
    {
        this.StringsList = new List<string> { "Newitem1", "Newitem2" };
    }
}

Upvotes: 1

taylorjonl
taylorjonl

Reputation: 929

ObservableCollection inherits from both INotifyPropertyChanged and INotifyCollectionChanged. I think if you want to know when the collection changed you should use this interface:

INotifyCollectionChanged

Upvotes: 0

Related Questions