Welcome to MSDN Blogs Sign in | Join | Help

Lester's WPF blog


Simple, easy & beautiful

News

Listening to DependencyProperty changes

Dependency property is a pretty kewl concept.  You got to agree on that J.  One the nice features is the ability to listen to the changes in these properties and I tend to use it a lot. The SDK way would be to derive from the control, override the dependencyproperty metadata and specify the propertychangedCallback in the signature. Hmmm… pretty cumbersome you would say.

public class MyTextBox : TextBox

    {

        public MyTextBox():base()

        {

        }

        static MyTextBox()

        {

            FlowDirectionProperty.OverrideMetadata(typeof(MyTextBox), new FrameworkPropertyMetadata(new PropertyChangedCallback(FlowDirectionPropertyChanged)));

 

        }

 

        private static void FlowDirectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)

        {

            ((MyTextBox)sender).FontWeight = (((MyTextBox)sender).FlowDirection == FlowDirection.RightToLeft) ? FontWeights.Bold : FontWeights.Normal;

        }

    }

But hey, things get easy, thanks to Ben. What you can do is use the DependencyPropertyDescriptor. 

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(DependencyProperty, typeof(Control)));
if (dpd != null)
{
    dpd.AddValueChanged(ControlInstance,
delegate
    {
       
// Add property change logic.
    });
}

 

So, as an example I just tried the following on a textbox. You change the flowDirection (Ctrl and Left Shift) and the text gets bold.... and yeah you need to have some RTL language such as Arabic installed. By default only English comes installed :)

DependencyPropertyDescriptor dpd = DependencyPropertyDescriptor.FromProperty(TextBox.FlowDirectionProperty, typeof(TextBox));
if (dpd != null)
{
    dpd.AddValueChanged(tb,
delegate
    {
        tb.FontWeight=(tb.FlowDirection==FlowDirection.RightToLeft)?FontWeights.Bold:FontWeights.Normal;

    });
}

 

That makes life a lot easier… doesn’t it!!

Share this post
Posted: Monday, March 05, 2007 4:30 PM by llester

Comments

mark said:

This is part of .net 3.0.

You should have mentioned it.

# March 8, 2007 5:15 AM

lester said:

Hi Mark,

Thats a good point.. It is however mentioned in the tags :)

# March 8, 2007 10:36 AM

di .NET e di altre Amenit said:

Appunti di WPF: Un po di link utili

# March 19, 2007 4:43 AM

10,000 Monkeys - Harnessing the Power of Typing Monkeys said:

Maybe not the best title to describe the post.  Anyway, I was looking at some of the apps the other...

# December 22, 2007 1:53 AM

10,000 Monkeys - Harnessing the Power of Typing Monkeys said:

Maybe not the best title to describe the post.  Anyway, I was looking at some of the apps the other...

# December 22, 2007 1:58 AM

10,000 Monkeys - Harnessing the Power of Typing Monkeys said:

Maybe not the best title to describe the post.  Anyway, I was looking at some of the apps the other...

# December 22, 2007 2:08 AM

10,000 Monkeys - Harnessing the Power of Typing Monkeys said:

Maybe not the best title to describe the post.  Anyway, I was looking at some of the apps the other...

# December 22, 2007 2:16 AM

10,000 Monkeys - Harnessing the Power of Typing Monkeys said:

Maybe not the best title to describe the post.  Anyway, I was looking at some of the apps the other...

# December 22, 2007 2:20 AM
New Comments to this post are disabled
Page view tracker