欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPF圆角按钮例程

程序员文章站 2022-05-20 11:37:55
......
<Window x:Class="WpfApp3.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp3"
        mc:Ignorable="d"
        Title="我的例程" Height="450" Width="800" Background="Black">
    <Window.Resources>

        <Style TargetType="Button" >
            <Setter Property="Template">
                
                <Setter.Value>
                    <ControlTemplate TargetType="Button" >
                        <Border x:Name="border" Background="SkyBlue" 
                                CornerRadius="10" Height="30" BorderBrush="Black" BorderThickness="0" SnapsToDevicePixels="True">
                            <Border.Effect>
                                <DropShadowEffect Color="Black" Direction="0" ShadowDepth="1" Opacity="1" />
                            </Border.Effect>
                        <TextBlock Text="{TemplateBinding Content}" 
                                  VerticalAlignment="Center" HorizontalAlignment="Center" Foreground="White" FontSize="16" FontFamily="黑体">
                                <TextBlock.Effect>
                                    <DropShadowEffect Color="Black" Direction="0" ShadowDepth="1" Opacity="1" />
                                </TextBlock.Effect>
                            </TextBlock>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Background" Value="LightBlue" TargetName="border"></Setter>
                            </Trigger> 
                            <Trigger Property="IsPressed" Value="true">
                            <Setter Property="Background" Value="SkyBlue" TargetName="border"></Setter>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
           
    </Style>
    </Window.Resources>
    <Grid Background="Brown" >
        <Button Content="暂停" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="445,344,0,0"></Button>
        <Button Content="添加" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75"  Margin="100,344,0,0"></Button>
        <Button Content="播放" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="219,344,0,0"></Button>
        <Button Content="开始" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="327,344,0,0" ></Button>
    </Grid>
</Window>

 这段代码直接使用VS全部粘贴就能够使用。分别演示了圆角,变色,阴影。 需要注意的是因为没有给style输入名字,导致所有的按钮都会被重画。