user626528
user626528

Reputation: 14417

Any way to get event, when some WPF data binding update occurs?

Any way to get event, when some WPF data binding update occurs?

UPD I'm developing a custom control extending one of standard controls. I need to get notification, when DataContext property of the ancestor control changes, or data binding causes update of any property of the ancestor control.

Upvotes: 2

Views: 6126

Answers (3)

HCL
HCL

Reputation: 36805

  • As others already mentioned, if your object implements INotifyPropertyChanged and the property supports it, register to PropertyChanged and you will be informed about changes.
  • If you are in a DependencyObject and add your own DependencyProperty, register a DependencyPropertyChangedEventHandler in the metadata to be informed when the property changes-
  • If you have a DependencyObject and the property is a DependencyProperty, you can override OnPropertyChanged. It will be called every time, a DependencyProperty value has been changed. You can then filter out the property-changes you are interested in.
  • If you are outside of a DependencyObject and want to listen to a changed value of a DepenendencyProperty, you can use the DependencyPropertyDescriptor to register for value changes. However take care, using this may produce memory-leaks.

Upvotes: 3

David Masters
David Masters

Reputation: 8295

If you're talking about getting a notification from a control perspective (i.e. when a dependency property has been bound to) you can provide a method that will be called passing the value:

public DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
                "ItemsSource",
                typeof(IList),
                typeof(CustomGridControl),
                new FrameworkPropertyMetadata(null, OnItemsSourceChanged));

private static void OnItemsSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    e.NewValue;
}

Upvotes: 2

tomahawk
tomahawk

Reputation: 249

It sounds like you require: INotifyPropertyChanged to be implemented on your View Model. This obviously depends on your implementation but this is assuming you've followed MVVM.

This then allows you to carry out some work based on the value of a bound property changing (and an event being raised).

Upvotes: 4

Related Questions