Reputation: 10513
I've got an Expander
style defined as follows:
<Style x:Key="MyStyle" TargetType="{x:Type Expander}">
<Setter ... />
<!-- More setters -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
<Border>
<DockPanel>
<ToggleButton x:Name="ChangeStyleHere" ... />
<Border ... />
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
I'd like to create an identical style, but change the Style
property of the inner ToggleButton
.
Can I do this without duplicating the style definition? Should I use the BasedOn
property?
Upvotes: 1
Views: 349
Reputation: 11051
If the control is your own, i would always add a new dependency property for the style, template etc. For example if you have a custom ItemsControl, where the template contains a ScrollViewer, i would add a dependency property ScrollViewerStyle
. If it isn't your control or one of the basic controls, you can always use attached properties for that. Something like
<TextBox myNs:TextBoxStyle.ScrollViewerStyle="{StaticResource myScrollViewer}"/>
TextBoxStyle
is just a simple class with one attached property called ScrollViewerStyle
. And of course your applied style and template must make use of this attached property. But after that you can easily change it on any TextBox.
Upvotes: 1
Reputation: 10513
Here's my current approach, I wonder if there is a better way:
<Style x:Key="MyStyle" TargetType="{x:Type Expander}">
<Setter ... />
<!-- More setters -->
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Expander}">
<Border>
<DockPanel>
<ToggleButton x:Name="ChangeStyleHere" Style="{DynamicResource InnerStyle}" />
<Border ... />
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style x:Key="DerivedStyle" TargetType="{x:Type Expander}" BasedOn="{StaticResource MyStyle}">
<Style.Resources>
<Style x:Name="InnerStyle" ... />
</Style.Resources>
</Style>
Upvotes: 1