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

标签 c# wpf styles stackpanel

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

谢谢..

最佳答案

您可以通过在 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>

关于c# - WPF-如何将样式应用于面板中的多个控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6212793/

相关文章:

c# - SqlCommandBuilder 内部连接更新

c# - 将 IntPtr 作为函数参数传递会导致内存泄漏吗?

c# - 从上下文菜单中获取所选菜单项的名称(或索引),该菜单项是通过绑定(bind)到 ObservableCollection 的 ItemsSource 动态生成的

c# - 移动 MainWindow.xaml

c# - 我是否需要持有至少 1 个 View 模型来处理我的所有 View ?

android - React Native - 选择多种样式

android - 为每个对话框更改对话框主题

C# 使用 SQL 使用不在 SQL Server 中的 DataTable 中的值更新数据库表

c# - 在 propertygrid 中使用字典

JavaScript DOM 样式