Wpf 容器中的隐式样式

Wpf 容器中的隐式样式,wpf,xaml,wpf-style,implicit-style,Wpf,Xaml,Wpf Style,Implicit Style,我想将Padding=“0”应用到左侧栏(第一个堆栈面板)上的每个标签上,使其与每个文本框(和其他控件)左对齐 如何在中定义仅适用于特定容器中的元素的隐式样式 备选方案: 使用x:Key=“sidebarLabel”。但是,对于我实际应用程序的侧栏中的许多标签来说,这个选项似乎是多余的 向侧边栏中的每个标签添加Padding=0。这与前面的备选方案基本相同 将隐式样式移动到。但是,我希望将样式(在App.xaml中)与xaml(在main window.xaml中)分开 名字: 姓氏:

我想将
Padding=“0”
应用到左侧栏(第一个
堆栈面板
)上的每个
标签上,使其与每个
文本框
(和其他控件)左对齐

如何在
中定义仅适用于特定容器中的元素的隐式样式

备选方案:

  • 使用
    x:Key=“sidebarLabel”
    。但是,对于我实际应用程序的侧栏中的许多标签来说,这个选项似乎是多余的
  • 向侧边栏中的每个标签添加
    Padding=0
    。这与前面的备选方案基本相同
  • 将隐式样式移动到
    。但是,我希望将样式(在
    App.xaml
    中)与xaml(在
    main window.xaml
    中)分开
  • 
    

    
    名字:
    姓氏:
    我想要这里的默认填充
    
    您可以在app.xaml中使用
    Style.Resources
    ,如下所示:

    <Application.Resources>
        <Style TargetType="StackPanel">
            <Style.Resources>
                <Style TargetType="Label">
                    <Setter Property="Padding" Value="0" />
                </Style>
            </Style.Resources>
        </Style>
    </Application.Resources>
    
    <Application.Resources>
        <Style TargetType="StackPanel" x:Key="LabelStyledPanel">
            <Style.Resources>
                <Style TargetType="Label">
                    <Setter Property="Padding" Value="0" />
                </Style>
            </Style.Resources>
        </Style>
    </Application.Resources>
    
    然后,所有使用静态资源标签样式面板的
    stackpanel
    都使用标签样式

    <Application.Resources>
        <Style TargetType="StackPanel">
            <Style.Resources>
                <Style TargetType="Label">
                    <Setter Property="Padding" Value="0" />
                </Style>
            </Style.Resources>
        </Style>
    </Application.Resources>
    
    <Application.Resources>
        <Style TargetType="StackPanel" x:Key="LabelStyledPanel">
            <Style.Resources>
                <Style TargetType="Label">
                    <Setter Property="Padding" Value="0" />
                </Style>
            </Style.Resources>
        </Style>
    </Application.Resources>