如何在WPF中为ToggleButton设置Border属性

时间:2015-01-05 22:16:22

标签: wpf styles togglebutton

我正试图设计一个ToggleButton。但由于一些奇怪的原因,我无法设置Border属性。边框不显示。

我得到的是:

enter image description here

我想要的是:

enter image description here

<Style x:Key="MyToggleButton" TargetType="ToggleButton">
    <Setter Property="SnapsToDevicePixels" Value="true" />
    <Setter Property="TextOptions.TextFormattingMode" Value="Display"/>
    <Setter Property="Foreground" Value="White"/>
    <Setter Property="Margin" Value="0 -1 0 0"/>
    <Setter Property="FontSize" Value="11"/>
    <Setter Property="BorderThickness" Value="0 1 1 0"/>
    <Setter Property="BorderBrush" Value="Green"/>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="ToggleButton">
                <Border x:Name="Border" 
                        Width="62" 
                        Height="19">
                    <ContentPresenter HorizontalAlignment="Center" 
                                      VerticalAlignment="Center" 
                                      Margin="-2 -3 0 0"
                                      RecognizesAccessKey="True" />
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter TargetName="Border" Property="Cursor" Value="Hand"/>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="False">
                        <Setter TargetName="Border" Property="Background">
                            <Setter.Value>
                                <ImageBrush TileMode="FlipY" ImageSource="pack://application:,,,/Main/Resources/dark/images/tab_normal_bg.png" />
                            </Setter.Value>
                        </Setter>
                    </Trigger>
                    <Trigger Property="IsChecked" Value="True">
                        <Setter TargetName="Border" Property="Background">
                            <Setter.Value>
                                <ImageBrush TileMode="FlipY" ImageSource="pack://application:,,,/Main/Resources/dark/images/tab_pressed_bg.png" />
                            </Setter.Value>
                        </Setter>

                    </Trigger>

                    <Trigger Property="IsChecked" Value="False">
                        <Setter Property="Foreground" Value="LightGray"/>
                    </Trigger>
                </ControlTemplate.Triggers>

            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

1 个答案:

答案 0 :(得分:2)

您必须实际使用控件模板中的属性BorderBrushBorderThickness。仅仅设置属性是不够的 - 框架不会对如何在模板中使用它们做任何假设。

            <Border x:Name="Border" 
                    Width="62" 
                    Height="19"
                    BorderBrush="{TemplateBinding BorderBrush}"
                    BorderThickness="{TemplateBinding BorderThickness}">