0

I'm working on a custom WPF UserControl and having an issue with one of my DependencyProperties.

So I built a test scenario that looks like this. In the Custom Control..

public static readonly DependencyProperty MyCollectionItemsSourceProperty = DependencyProperty.Register("DynamicHeaderItemsSource", typeof(IEnumerable), typeof(TestUserControl1),
new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender, new PropertyChangedCallback(OnMyCollectionItemsSourceChanged))); 

public IEnumerable MyCollectionItemsSource
{
    get { return (IEnumerable)GetValue(MyCollectionItemsSourceProperty ); }
    set { SetValue(MyCollectionItemsSourceProperty , value); }
}

protected static void OnMyCollectionItemsSourceChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
{
    System.Diagnostics.Debug.WriteLine("MyCollection Updated");           
}

In my test window's code behind:

public ObservableCollection<string> MyTestStrings { get; set; }

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    MyTestStrings.Add("First");
    MyTestStrings.Add("Second");
    MyTestStrings.Add("Third");
}

And in my test window's XAML:

<Grid>
    <local:TestUserControl1 MyCollectionItemsSource="{Binding MyTestStrings}">
</Grid>

The problem is, I never get a notification of any type when the underline collection changes. The OnMyCollectionItemsSourceChanged only ever gets called once: at the beginning when the binding is set. What am I missing?

Hexum064
  • 349
  • 2
  • 14

2 Answers2

2

It is an expected behavior your MyCollectionItemsSource just change when it is set in XAML binding since (one time )t hen those adds in the collection is not changing your property itself (it is doing something inside of the collection). if you want to get information about changing collection you have to first in OnMyCollectionItemsSourceChanged event test if the vale supports INotifyCollectionChanged this then register for NotifyCollectionChangedEventHandler isndie, do not forget to unregister your handler

  protected static void OnMyCollectionItemsSourceChanged(DependencyObject property, DependencyPropertyChangedEventArgs args)
    {
        if( args.OldValue is INotifyCollectionChanged)
           (args.OldValue as INotifyCollectionChanged ).CollectionChanged -= CollectionChangedHandler;
       if(args.NewValue is INotifyCollectionChanged)
           (args.OldValue as INotifyCollectionChanged).CollectionChanged += CollectionChangedHandler;

    }

    private static void CollectionChangedHandler(object sender, NotifyCollectionChangedEventArgs e)
    {
      //
    }
Mojtaba
  • 1,210
  • 2
  • 12
  • 29
1

The PropertyChangedCallback will be called only when the property is set (or nullified) not if there are any changes to the collection itself (adding/removing elements). To do that you will have to hook up to the CollectionChanged event. See this post: https://stackoverflow.com/a/12746855/4173996

Community
  • 1
  • 1
Jerrington
  • 113
  • 4