In WPF, how would I apply multiple styles to a FrameworkElement
? For instance, I have a control which already has a style. I also have a separate style which I would like to add to it without blowing away the first one. The styles have different TargetTypes, so I can't just extend one with the other.
.net – How to apply multiple styles in WPF
netstyleswpf
Related Topic
- C# – Catch multiple exceptions at once
- .net – Handling Dialogs in WPF with MVVM
- C# – How to get a consistent byte representation of strings in C# without manually specifying an encoding
- Wpf – How to match the controls’ styles to the current theme? (WPF)
- Wpf – How to bind inverse boolean properties in WPF
- Android – Is it possible to have multiple styles inside a TextView
- Scala: list.flatten: no implicit argument matching parameter type (Any) = > Iterable[Any] was found
Best Answer
I think the simple answer is that you can't do (at least in this version of WPF) what you are trying to do.
That is, for any particular element only one Style can be applied.
However, as others have stated above, maybe you can use
BasedOn
to help you out. Check out the following piece of loose xaml. In it you will see that I have a base style that is setting a property that exists on the base class of the element that I want to apply two styles to. And, in the second style which is based on the base style, I set another property.So, the idea here ... is if you can somehow separate the properties that you want to set ... according the inheritance hierarchy of the element you want to set multiple styles on ... you might have a workaround.
Hope this helps.
Note:
One thing in particular to note. If you change the
TargetType
in the second style (in first set of xaml above) toButtonBase
, the two Styles do not get applied. However, check out the following xaml below to get around that restriction. Basically, it means you need to give the Style a key and reference it with that key.