如何在 WPF 中为 ToggleButton 设置边框 属性

How to set Border property for ToggleButton in WPF

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

我得到的是:

我想要的是:

<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>

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

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