【问题标题】:Button Style in Resource not Applied to Buttons资源中的按钮样式未应用于按钮
【发布时间】:2014-12-12 15:57:45
【问题描述】:

有人可以向我解释为什么这不起作用吗? ToolBar 中的按钮未获取 Black BorderBrush 属性设置。我试过 TargetType="Button" 和 TargetType="{x:Type Button}" 但都不起作用。我对一系列标签做了几乎完全相同的事情,而且效果很好。我对 WPF 很陌生。这里有什么我对样式优先级不了解的地方吗?谢谢!

...Window Definition...

<Grid>
    <Grid.Resources>
        <Style TargetType="{x:Type Button}">
            <Setter Property="BorderBrush" Value="Black" />
        </Style>
    </Grid.Resources>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>
    <ToolBar Grid.Column="0" Grid.Row="0" Margin="0">
        <Button>
            <StackPanel Orientation="Horizontal" Height="Auto">
                <Rectangle Width="16" Height="16" Fill="LightBlue" VerticalAlignment="Center"></Rectangle>
                <Label Padding="0" VerticalAlignment="Center" HorizontalAlignment="Left">Redraw</Label>
            </StackPanel>
        </Button>
        ... More Buttons ...
    </ToolBar>
</Grid>

... End Window Definition ...

【问题讨论】:

    标签: wpf


    【解决方案1】:

    给你

    <Style x:Key="{x:Static ToolBar.ButtonStyleKey}"
           TargetType="{x:Type Button}">
        <Setter Property="BorderBrush"
                Value="Black" />
    </Style>
    

    来自How to: Style Controls on a ToolBar

    工具栏定义 ResourceKey 对象来指定控件的样式 在工具栏中。要为 ToolBar 中的控件设置样式,请将样式的 x:key 属性设置为 ToolBar 中定义的 ResourceKey。

    ToolBar 定义了以下 ResourceKey 对象:

    • 按钮样式键
    • CheckBoxStyleKey
    • 组合框样式键
    • 菜单样式键
    • 单选按钮样式键
    • SeparatorStyleKey
    • 文本框样式键
    • 切换按钮样式键

    【讨论】:

    • 啊,有道理。我想我应该多阅读这些文档 :) 感谢您的回答!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-20
    • 1970-01-01
    • 2014-07-05
    • 2014-05-29
    相关资源
    最近更新 更多