How to apply multiple styles in WPF
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
WPF does not support applying multiple styles to a single element directly — a FrameworkElement has one Style property, not a list. However, there are several patterns to combine styles: the BasedOn property for style inheritance, merged ResourceDictionary for organizing styles, a custom StyleMerger markup extension, and attaching additional setters via attached behaviors. The most common and recommended approach is BasedOn, which creates a chain of inherited styles.
Style Inheritance with BasedOn
BasedOn lets one style inherit all setters from a parent style and add or override properties:
PrimaryButtonStyle inherits FontSize, Padding, and Margin from BaseButtonStyle and adds its own Background and Foreground.
Multi-Level Style Chain
You can chain multiple levels of BasedOn:
Implicit Styles with BasedOn
Implicit styles (no x:Key) apply to all controls of a given type. You can combine implicit and explicit styles:
The BasedOn="{StaticResource {x:Type TextBox}}" syntax references the implicit style for TextBox.
Custom Markup Extension for Merging Styles
For cases where BasedOn chains are not flexible enough, you can create a markup extension that merges multiple styles at runtime:
This approach is a workaround — use it only when BasedOn chains cannot express the desired combination.
Attached Behavior Pattern
Another approach uses attached properties to add supplementary styling:
Common Pitfalls
- Trying to set the
Styleproperty twice on one element: XAML does not support setting the same property twice.<Button Style="{StaticResource A}" Style="{StaticResource B}"/>causes a compile error. UseBasedOnor a markup extension instead. - Circular
BasedOnreferences: Style A based on Style B based on Style A creates an infinite loop that crashes at runtime with aStackOverflowException. Always structure style inheritance as a directed acyclic chain. BasedOnwith mismatchedTargetType: AButtonstyle can be based on aControlorButtonBasestyle (ancestor types), but not on aTextBoxstyle. The base style'sTargetTypemust be the same type or an ancestor of the derived style'sTargetType.- Overriding setters unintentionally: When a derived style sets the same property as the base style, the derived value wins. This is usually desired, but forgetting that a base style sets a property can lead to confusion when your explicit setter seems to be ignored.
- Performance with dynamic style merging: Custom markup extensions that merge styles at runtime create new
Styleobjects for each element. In data-bound lists with thousands of items, this can cause measurable allocation overhead. Prefer staticBasedOnchains for performance-critical scenarios.
Summary
- WPF elements can only have one
Style— useBasedOnto inherit and extend styles in a chain - Create a base style with shared properties, then derive specialized styles from it
- Reference implicit styles in
BasedOnwith{StaticResource {x:Type TargetType}} - For advanced scenarios, use a custom
MarkupExtensionor attached behaviors to merge multiple styles at runtime - Always prefer
BasedOnchains over runtime merging — they are simpler, faster, and easier to maintain - Avoid circular
BasedOnreferences and ensureTargetTypecompatibility in the chain
Related reading
- How to Async Files.ReadAllLines and await for results?
- How to asynchronously iterate an ObservableCollection containing hierarchical elements?
- How to automatically select all text on focus in WPF TextBox?
- How to await an IAsyncAction method?
- How to bind an enum to a combobox control in WPF?
- How to build a query string for a URL in C?
- How to bust the cache or obtain cache key when using <distributed-cache> Tag helper in Asp.net Core MVC
- How to calculate pi to N number of places in C using loops

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.