Reputation: 21
I need to apply style to different controls within a Stack Panel. They are all of different type i.e. TreeView,Listview,ComboBox etc. Is there a way I can apply a style at StackPanel level to be applicable for these controls. I don't want to apply style individually to these controls. Is there any way to accomplish this?
Thanks..
Upvotes: 2
Views: 1008
Reputation: 1612
You can do it like this by declaring the Styles withing the StackPanel Resources. You have to declare each Style without a key for them to be automatically applied to every target control within the StackPanel.
<StackPanel>
<StackPanel.Resources>
<!-- Styles declared here will be scoped to the content of the stackpanel -->
<!-- This is the example of style declared without a key, it will be applied to every TreeView. Of course you'll have to add Setters etc -->
<Style TargetType="TreeView">
</Style>
</StackPanel.Resources>
<!-- Content -->
<!-- This treeview will have the style declared within the StackPanel Resources applied to it-->
<TreeView />
</StackPanel>
Upvotes: 3
Reputation: 15999
As Jean-Louis says, you can specify a Style
within the StackPanel
resource dictionary and it will only be applied to matching elements within that StackPanel
.
In order to have a single Style
match all of your controls, you will need to specify it with a TargetType
of a common base class for all of those controls, such as Control
<StackPanel>
<StackPanel.Resources>
<Style TargetType="Control">
<!-- Setters etc here -->
</Style>
</StackPanel.Resources>
<!-- Controls here -->
</StackPanel>
Upvotes: 0