Debdeep Debdeep - 2 months ago 13x
C# Question

WPF override dependency property metdata is not working

I have a requirement to monitor the topmost propertyvalue change on WPF Window. I am writing something like this:

static MainWindow()
TopmostProperty.OverrideMetadata(typeof(Window), new PropertyMetadata(new PropertyChangedCallback(Changed)));

public MainWindow()

private static void Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
throw new NotImplementedException();

But I am getting this exception:
he invocation of the constructor on type 'WpfApplication4.MainWindow' that matches the specified binding constraints threw an exception.' Line number '4' and line position '9'."


Two mistakes:

  1. The first argument of OverrideMetadata must be your type.

  2. The type of the second argument must be the same as in the base type.

        new FrameworkPropertyMetadata(Changed));
  3. (Bonus) You don't need to override metadata if you just need change notification.