tags:

views:

148

answers:

2

I want to be able to apply a style at runtime to an object ONLY if the current style is the default style. I don't want to override any user defined styles. Anyone know how to do this?

A: 

Check the DefaultStyleKeyProperty, which is a static property of any custom control.

string styleKeyName = DefaultStyleKeyProperty.Name;

Usually if there is no style associated with the control, the Name will be "DefaultStyleKey"

Jobi Joy
+2  A: 

It appears you can do it this way:

DependencyPropertyHelper.GetValueSource(
    someControl, FrameworkElement.StyleProperty).BaseValueSource 
    == BaseValueSource.Default;

You can wrap that up in an extension method like this:

static public bool HasDefaultStyle(this FrameworkElement item)
{
    return DependencyPropertyHelper.GetValueSource(
        item, FrameworkElement.StyleProperty).BaseValueSource 
        == BaseValueSource.Default;
}

Then you can just call someControl.HasDefaultStyle().

Also, have a look at this article: Default Templates in WPF

Kyralessa
Bonus points for the extension method idea!!
Micah