开发者

WPF- How to apply Style to Multiple controls within a Panel

开发者_如何学JAVAI 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..


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>


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜