代码之家  ›  专栏  ›  技术社区  ›  Meh

为什么不应用以下WPF样式?

  •  2
  • Meh  · 技术社区  · 15 年前

    为什么下面的样式不起作用?我在其他地方有完全相同的结构,不同的控制,它的工作。

    使用 TargetType="{x:Type Separator}" 而不是 TargetType="Separator" 也不管用。不动 Style 在XAML层次结构的上部。我能避免使用 x:Key="styleName" 和 Style="{StaticResource styleName}" ?

    但更重要的是,我试图理解为什么这样的风格绑定并不总是有效。

    <ToolBar Height="25">
        <ToolBar.Resources>
            <Style TargetType="Separator">
                <Setter Property="Margin" Value="15"/>
            </Style>
            <Style TargetType="RadioButton">
                <Setter Property="Margin" Value="10"/>
                <Setter Property="Width" Value="50"/>
            </Style>
        </ToolBar.Resources>
    
        <RadioButton Content="A"/>  <!-- NOT APPLIED HERE -->
        <Separator/>                <!-- NOT APPLIED HERE -->
        <RadioButton Content="B"/>  <!-- NOT APPLIED HERE -->
    </ToolBar>
    
    1 回复  |  直到 15 年前
        1
  •  3
  •   Matt Hamilton    15 年前

    WPF的工具栏用它的子控件做了一些有趣的事情。与其他容器不同,它不像定义应用于类型的样式那么简单。

    看看这篇博文可以得到一些有用的信息。 Styling controls on a ToolBar .