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?
views:
148answers:
2
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
2008-12-03 22:01:29
+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
2008-12-03 22:16:17
Bonus points for the extension method idea!!
Micah
2008-12-10 20:31:52