【C#】WPF字典资源设置Button控件样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="BtnInfoStyle" TargetType="Button">
        <Setter Property="Width" Value="80" />
        <Setter Property="Height" Value="40" />
        <Setter Property="Foreground" Value="White" />
        <Setter Property="BorderThickness" Value="0" />
        <Setter Property="Background" Value="#43a9ff" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border
                        x:Name="border"
                        Margin="2"
                        Background="{TemplateBinding Background}"
                        BorderBrush="{TemplateBinding BorderBrush}"
                        BorderThickness="{TemplateBinding BorderThickness}"
                        CornerRadius="5"
                        SnapsToDevicePixels="True">
                        <TextBlock
                            HorizontalAlignment="Center"
                            VerticalAlignment="Center"
                            Foreground="{TemplateBinding Foreground}"
                            Text="{TemplateBinding Content}" />
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter TargetName="border" Property="Background" Value="#2f96b4" />
                            <Setter Property="TextBlock.Cursor" Value="Hand" />
                        </Trigger>
                        <Trigger Property="IsPressed" Value="True">
                            <Setter TargetName="border" Property="Background" Value="#2a89a4" />
                        </Trigger>
                        <!--<Trigger Property="IsEnabled" Value="True">
                            <Setter TargetName="border" Property="Background" Value="#2f96b4" />
                        </Trigger>-->
                        <Trigger Property="IsEnabled" Value="False">
                            <Setter TargetName="border" Property="Background" Value="#C0C0C0" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>
posted @ 2024-05-20 11:25  qiutian-hao  阅读(28)  评论(0编辑  收藏  举报