views:

334

answers:

2

I have a dependency property (Foreground) on a custom control which is inheriting from Control. When I am loading the control, I want to see if the user has set the dependency property or if it is using its default value before I set the value for them. The problem is a bit more complicated then that, here it is:

The control is in a custom control library that a user adds to their project. When they are setting up their project, I have the ability for them to add a line to their app.xaml file with a given x:key that contains the settings that I read while setting the default values for my controls. However, if they set the value either in code, or in xaml, I do not want to set the value with this global value. Here are a few cases to help visualize what I'm talking about:

  1. The user creates a control from my library and puts it on their page. They then set the foreground to green. I want the value of the foreground to be green for that control.

  2. The user adds a special line to their app.xaml that states they want all controls of a particular kind to have a foreground of red. I want the value of the foreground of all controls of that kind to be red.

  3. The user adds a special line to their app.xaml that states they want all controls of a particular kind to have a foreground of red. They then set the value of the foreground of a single control (in xaml) to yellow. I want the value of the foreground of all controls of that kind to be red, except for the one they otherwise specified to be yellow.

When loading the control, I want to read the dependency property to see if they have set the value, or if it is the default value which is defined in the style. If it is the default value, I want to then read their special line out of the app.xaml file and set the fore ground to that color.

A: 

I've done a bit more research and believe that I've found an answer. When I am loading the control (or updating) I check the type of ReadLocalValue(ForegroundProperty) if this type is System.Object then it is not set in xaml. However, if this type is something other then System.Object (Like System.Windows.Media.Brush) then it is set in xaml.

Scott
A: 

Extending your solution to complete, from MSDN page ReadLocalValue:

if (ReadLocalValue(FontSizeProperty) == DependencyProperty.UnsetValue)
    // Do your thing...

That is, there exists DependencyProperty.UnsetValue against which to check if the value is set or not.

Ciantic