locked
Detect changes for an already existing property RRS feed

  • Question

  • I am extending a textblock element, and in the specific I have to detect changes of its ForegroundProperty.

    How can I do this ?

    for a custom property I write something like this:

    public static readonly DependencyProperty MyProperty =
                DependencyProperty.RegisterAttached("Property",
                typeof(bool), typeof(MyClass), new PropertyMetadata(false, PropertyChanged));

    What if a property already exists?

    Thank you 

    Friday, February 22, 2013 2:40 PM

Answers

  • For existing DPs you might use OverrideMetadata or AddOwner and pass new Metadata instance with your PropertyChanged callback. OverrideMetadata is more appropriate if your class inherits from the class whose DP you want observe for changes. AddOwner is fore cases where there is no inheritance between new on and original owner


    Chris


    public class MyTextBlock : Control
    {
        static MyTextBlock()
        {         TextBlock.ForegroundProperty.AddOwnwer(typeof(MyTextBlock), new FrameworkPropertyMetadata(Brushes.Black, OnForegroundChanged)
        }
    
        private static void OnForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       { 
            var mtb = (MyTextBlock) d;
            //do whatever with mtb and e */
       }
    }

    • Edited by Chris-von-der-Wiese Saturday, February 23, 2013 10:12 AM add example
    • Marked as answer by F Starred Saturday, February 23, 2013 4:42 PM
    Friday, February 22, 2013 6:56 PM

All replies

  • What you want to do is the following:

    Get a descriptor of the property then add a AddValueChanged handler for that property.

    Example (VB)

                Dim descriptor As DependencyPropertyDescriptor = DependencyPropertyDescriptor.FromProperty(ListView.ViewProperty, GetType(ListView))
                descriptor.AddValueChanged(lv, New EventHandler(AddressOf hiddenPanel_VisibilityChanged))
    

    Hope this helps

    LS


    Lloyd Sheen

    • Proposed as answer by Jürgen Röhr Friday, February 22, 2013 6:18 PM
    Friday, February 22, 2013 4:55 PM
  • For existing DPs you might use OverrideMetadata or AddOwner and pass new Metadata instance with your PropertyChanged callback. OverrideMetadata is more appropriate if your class inherits from the class whose DP you want observe for changes. AddOwner is fore cases where there is no inheritance between new on and original owner


    Chris


    public class MyTextBlock : Control
    {
        static MyTextBlock()
        {         TextBlock.ForegroundProperty.AddOwnwer(typeof(MyTextBlock), new FrameworkPropertyMetadata(Brushes.Black, OnForegroundChanged)
        }
    
        private static void OnForegroundChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
       { 
            var mtb = (MyTextBlock) d;
            //do whatever with mtb and e */
       }
    }

    • Edited by Chris-von-der-Wiese Saturday, February 23, 2013 10:12 AM add example
    • Marked as answer by F Starred Saturday, February 23, 2013 4:42 PM
    Friday, February 22, 2013 6:56 PM
  • Sadly I got an exception when call AddOwner or OverridingMetadata, but regardless my problem I think this is what I was looking for
    Saturday, February 23, 2013 4:43 PM