WPF-如何将样式应用于面板中的多个控件

发布于 2024-11-12 07:35:23 字数 149 浏览 4 评论 0原文

我需要将样式应用于堆栈面板中的不同控件。它们都有不同的类型,即 TreeView、Listview、ComboBox 等。 有没有办法可以在 StackPanel 级别应用适用于这些控件的样式。 我不想将样式单独应用于这些控件。 有什么办法可以做到这一点吗?

谢谢..

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

·深蓝 2024-11-19 07:35:23

您可以通过在 StackPanel 资源中声明样式来做到这一点。您必须在没有键的情况下声明每个样式,以便它们自动应用于 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>

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>
绻影浮沉 2024-11-19 07:35:23

正如 Jean-Louis 所说,您可以在 StackPanel 资源字典中指定 Style,它只会应用于该 StackPanel 中的匹配元素。

为了使单个 Style 与所有控件相匹配,您需要使用所有这些控件的公共基类的 TargetType 来指定它,例如 <代码>控制

<StackPanel>
  <StackPanel.Resources>
    <Style TargetType="Control">
      <!-- Setters etc here -->
    </Style>
  </StackPanel.Resources>

<!-- Controls here -->

</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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文