1

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()
        {
            InitializeComponent();
        }

        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'."

Athari
  • 33,702
  • 16
  • 105
  • 146
Debdeep
  • 241
  • 1
  • 15

1 Answers1

5

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.

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

Community
  • 1
  • 1
Athari
  • 33,702
  • 16
  • 105
  • 146
  • I didn't know about #2 - is it documented anywhere? – Adi Lester Sep 24 '14 at 20:58
  • 2
    @AdiLester See [Framework Property Metadata - Specifying Metadata](http://msdn.microsoft.com/en-us/library/ms751554.aspx#Specifying_Metadata): `For existing properties (AddOwner or OverrideMetadata call), you should always override with the metadata type used by the original registration`. – Clemens Sep 24 '14 at 21:01