Jan 22

Clearing values of dependency properties

Posted in Silverlight      Comments Off on Clearing values of dependency properties

In the previous posts, we’ve met several interesting features associated with dependency properties. I’m not sure if you’ve really had the time to understand the implications of having multiple “providers” (or if my writing was good enough for making you take into account those implications – which probably is true!), so I’ve though about writing a post on how to correctly clear the value of a dependency property. Suppose we’ve got the following XAML:

<UserControl x:Class="Tests.MainPage"
"" xmlns:x="" FontSize="30"> <UserControl.Resources> <Style TargetType="TextBlock"> <Setter Property="Foreground" Value="Blue" /> <Setter Property="Height" Value="100" /> </Style> </UserControl.Resources> <TextBlock Text="Hello, world!" x:Name="info" /> </UserControl>

And that we’ve used the following C# code for handling mouse enter/leave events:

public MainPage() {
  info.MouseEnter += (sender, e) =>
    info.Foreground = new SolidColorBrush(Colors.Red);
  info.MouseLeave += (sender, e) =>
    info.Foreground = new SolidColorBrush(Colors.Blue);

And yes, you’re right: we don’t need the { } to delimiter the instructions in the previous lambdas. Now that we’ve moved that out of the way, it’s time to remember what we’ve discussed in the previous post: the order by which you can influence the value of a dependency property. As we’ve seen, the value of a dependency property can be influenced by several “providers” where each “provider” might end up overriding the value set by another “provider” (it all depends on the priority of each provider!).

If you pay attention to the code (XAML), you’ll notice that the initial Foreground color of the TextBlock is set from a style setter. However, the code used to handle the MouseEnter/MouseLeave ends up setting the *local* value of the dependency property. In this case, there really isn’t any problem because we’re setting the local value of the dependency property to the same value as the one we had in the style setter. However,that is probably not what the author intended…He probably would really love to be able to just change the color to red on enter and go back to the previous existing color (whatever that color is) when the mouse exits the area delimited by the TextBlock.

This feature is known as “cleaning the value of a dependency property” and there’s a method for doing that: ClearValue. Take a look at the rewritten MouseLeave event handler:

info.MouseLeave += (sender,e) => 

As you can see, the ClearValue method expects a reference to an existing DependencyProperty object (in this example, that would be the ForegroundProperty field of the TextBlock class) which ends up being cleared. Whenever you need to do something like this (clear a value which has been temporarily set), you should always use the ClearValue method. Notice that using this method lets you change styles dynamically and you’ll always get the correct color when the mouse leaves the element.

And I guess that is all for now. Stay tuned for more on Silverlight.