Debdeep
Debdeep

Reputation: 241

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()
        {
            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'."

Upvotes: 1

Views: 1069

Answers (1)

Athari
Athari

Reputation: 34293

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.

Upvotes: 5

Related Questions