Wpf一个好看的按钮样式

如图

按钮样式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:WpfApplication1">
    <Style x:Key="Window" TargetType="Window">
        <Setter Property="WindowStyle" Value="None"/>
        <Setter Property="AllowsTransparency" Value="True"/>
        <Setter Property="Background" Value="Transparent"/>
    </Style>
    <Style x:Key="BUTTON_MENUBAR" TargetType="Button">
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="HorizontalAlignment" Value="Right"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                        <Grid Name="g" Opacity="0" Background="LightGray"/>
                        <Grid Name="grd" RenderTransformOrigin="0.5,0.5" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" VerticalAlignment="Center" HorizontalAlignment="Center" >
                            <Grid.RenderTransform>
                                <TransformGroup>
                                    <!--<RotateTransform x:Name="rotate" Angle="0"-->
                                    <ScaleTransform x:Name="scale" ScaleX="0.8" ScaleY="0.8"/>
                                </TransformGroup>
                            </Grid.RenderTransform>
                        </Grid>
                        <ContentPresenter HorizontalAlignment="Left" VerticalAlignment="Center" />
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0.2"  Duration="0:0:0.2" Storyboard.TargetName="g" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation To="1"  Duration="0:0:0.2" Storyboard.TargetName="scale" Storyboard.TargetProperty="ScaleX" />
                                        <DoubleAnimation To="1"  Duration="0:0:0.2" Storyboard.TargetName="scale" Storyboard.TargetProperty="ScaleY" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0"  Duration="0:0:0.2" Storyboard.TargetName="g" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation To="0.8"  Duration="0:0:0.2" Storyboard.TargetName="scale" Storyboard.TargetProperty="ScaleX" />
                                        <DoubleAnimation To="0.8"  Duration="0:0:0.2" Storyboard.TargetName="scale" Storyboard.TargetProperty="ScaleY" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="BUTTON_MENUBAR_PATH" TargetType="Button">
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="HorizontalAlignment" Value="Left"/>
        <Setter Property="Foreground" Value="Gray" />
        <Setter Property="Height" Value="30"/>
        <Setter Property="Width" Value="Auto"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                        <Grid Name="g" Background="LightGray" Opacity="0" />
                        <Grid Name="grd"  Width="22" Height="22" Background="{TemplateBinding Background}" VerticalAlignment="Center" HorizontalAlignment="Center"/>
                        <ContentPresenter VerticalAlignment="Center" HorizontalAlignment="Center" />
                        <Path Width="10" Height="30" HorizontalAlignment="Right" VerticalAlignment="Center" Data="M3,10 L7,15 L3,20" Stroke="Gray" StrokeThickness="1"/>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0.4"  Duration="0:0:0.2" Storyboard.TargetName="g" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.2" Storyboard.TargetName="g" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>

                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="BUTTON_MENUBAR_MINI" TargetType="Button">
        <Setter Property="VerticalAlignment" Value="Top"/>
        <Setter Property="HorizontalAlignment" Value="Right"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type Button}">
                    <Grid  Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" ClipToBounds="True">
                        <Border Name="bdr" BorderBrush="LightGray" BorderThickness="2" Opacity="0">
                            <Border.Effect>
                                <DropShadowEffect x:Name="effect" BlurRadius="20" Opacity="0.8" ShadowDepth="0"  Color="LightGray"/>
                            </Border.Effect>
                        </Border>
                        <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Background="{TemplateBinding Background}" VerticalAlignment="Center" HorizontalAlignment="Center" >
                            
                        </Grid>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="1"  Duration="0:0:0.3" Storyboard.TargetName="bdr" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0"  Duration="0:0:0.3" Storyboard.TargetName="bdr" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

    
    <Style x:Key="BUTTON_AGREE" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border Name="bdr" CornerRadius="3" Opacity="0.5" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="1">
                        <!--可使用ContentPresenter代替-->
                        <!--Foreground的值White可以替换为{TemplateBinding Foreground}-->
                        <Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Foreground="White" Content="{TemplateBinding Content}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0.7"  Duration="0:0:0.2" Storyboard.TargetName="bdr" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0.5"  Duration="0:0:0.2" Storyboard.TargetName="bdr"  Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="BUTTON_DISAGREE" TargetType="{x:Type Button}">
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="3" BorderBrush="#FFEBEBEB" BorderThickness="1">
                        <Grid>
                            <Border Name="bdr" CornerRadius="3" Background="Gray" Opacity="0"/>
                            <Label VerticalContentAlignment="Center" HorizontalContentAlignment="Center" Foreground="{TemplateBinding Foreground}" Content="{TemplateBinding Content}"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0.2"  Duration="0:0:0.2" Storyboard.TargetName="bdr" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0"  Duration="0:0:0.2" Storyboard.TargetName="bdr" Storyboard.TargetProperty="Opacity" />
                                    </Storyboard>
                                </BeginStoryboard>

                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    
    
    <SolidColorBrush x:Key="SOLIDCOLORBRUSH_LIGHT" Color="#FF6FD1FF"/>
    <Color x:Key="COLOR_LIGHT" R="111" G="209" B="255" A="255" />
    
    
    <Style x:Key="BUTTON_ELLIPSE" TargetType="{x:Type Button}">
        <Setter Property="Cursor" Value="Hand"/>
        <Setter Property="ToolTip" Value="下一步"/>

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border CornerRadius="15" Width="30" Height="30" Background="{DynamicResource SOLIDCOLORBRUSH_LIGHT}">
                        <Border.Effect>
                            <DropShadowEffect x:Name="effect" BlurRadius="7" Opacity="0.6" ShadowDepth="0"  Color="{DynamicResource COLOR_LIGHT}"/>
                        </Border.Effect>
                        <Grid>
                            <Path Name="path" HorizontalAlignment="Left"  Margin="0,0,0,0" Data="M5,15 L 15,23 L24,9" Stroke="White" StrokeThickness="1"/>
                            <Path Name="path2" HorizontalAlignment="Left" Opacity="0" Margin="0,0,0,0" Data="M5,15 H25 L17,7 M25,15 L17,22 " Stroke="White" StrokeThickness="1"/>
                        </Grid>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Trigger.EnterActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation To="0.9"  Duration="0:0:0.3" Storyboard.TargetName="effect" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation To="15"  Duration="0:0:0.3" Storyboard.TargetName="effect" Storyboard.TargetProperty="BlurRadius" />
                                        <DoubleAnimation To="0"  Duration="0:0:0.5" Storyboard.TargetName="path" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation To="1"  Duration="0:0:0.5" BeginTime="0:0:0.3" Storyboard.TargetName="path2" Storyboard.TargetProperty="Opacity" />

                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>
                            <Trigger.ExitActions>
                                <BeginStoryboard >
                                    <Storyboard>
                                        <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetName="effect" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation Duration="0:0:0.3" Storyboard.TargetName="effect" Storyboard.TargetProperty="BlurRadius" />
                                        <DoubleAnimation Duration="0:0:0.5" BeginTime="0:0:0.3" Storyboard.TargetName="path" Storyboard.TargetProperty="Opacity" />
                                        <DoubleAnimation Duration="0:0:0.5" Storyboard.TargetName="path2" Storyboard.TargetProperty="Opacity" />

                                    </Storyboard>
                                </BeginStoryboard>

                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>

    </Style>

</ResourceDictionary>

主界面

<Window x:Class="WpfApplication1.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:WpfApplication1"
        mc:Ignorable="d"
        WindowStyle="None"        
        Height="230" 
        Width="600" 
        Title="读取dbf"
        Style="{StaticResource Window}"
        WindowStartupLocation="CenterScreen"     
        Icon="Resources\ylj2.png"
        ShowInTaskbar="True"
        x:Name="UserControl1"
        AllowsTransparency="True" Background="Transparent" OpacityMask="White" ResizeMode="NoResize" >
    <Window.Resources>
    </Window.Resources>
    <Grid Background="Transparent" Margin="0">


        <Border  CornerRadius="5,5,5,5">
            <Border.Effect>
                <DropShadowEffect ShadowDepth="0"
                                  Color="LightGray"
                                  BlurRadius="12"
                                  Opacity="0.5"/>
            </Border.Effect>
            <Image Source="Resources\background.png"
                   Stretch="Fill"
                   />
        </Border>
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="30"></RowDefinition>
                <RowDefinition></RowDefinition>
                <RowDefinition></RowDefinition>
            </Grid.RowDefinitions>
            <Grid Grid.Row="0">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30"></ColumnDefinition>
                    <ColumnDefinition></ColumnDefinition>
                    <ColumnDefinition Width="360"></ColumnDefinition>
                    <ColumnDefinition Width="30"></ColumnDefinition>
                    <ColumnDefinition Width="30"></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Grid.Column="1"
                        x:Name="btn_OpenDbf"
                        Style="{StaticResource BUTTON_MENUBAR_PATH}" 
                        Background="{x:Null}"
                        Content="打开dbf文件   " 
                        FontSize="15"
                        VerticalAlignment="Bottom"
                        Click="btn_OpenDbf_Click"
                        Margin="0,0,0,0"/>
                <Grid Grid.Column="2">
                    <TextBlock    x:Name="txt_FilePath"
                                  Margin="0,0,0,5"
                                  VerticalAlignment="Bottom"
                                  Foreground="Gray"
                                  Text="">
                    </TextBlock>
                    <Line X1="0" Y1="30" X2="365" Y2="30"  StrokeEndLineCap="Round"   StrokeThickness="2">
                        <Line.Stroke>
                            <LinearGradientBrush EndPoint="0,0.5" StartPoint="1,1">
                                <GradientStop Color="Gray"/>
                                <GradientStop Offset="100"/>
                            </LinearGradientBrush>
                        </Line.Stroke>
                    </Line>
                </Grid>



                <Button Grid.Column="3" 
                        Style="{StaticResource  BUTTON_MENUBAR}"
                        VerticalAlignment="Top"
                        HorizontalAlignment="Right"
                        Width="30" Height="30">
                    <Button.Background>
                        <ImageBrush ImageSource="Resources/setting.png"/>
                    </Button.Background>
                </Button>

                <Button Grid.Column="4" 
                        x:Name="btn_Close"
                        Style="{StaticResource  BUTTON_MENUBAR_MINI}" 
                        VerticalAlignment="Top"
                        HorizontalAlignment="Right" 
                        Width="30"
                        Height="30" Click="btn_Close_Click" >
                    <Button.Background>
                        <ImageBrush ImageSource="Resources/close.png"/>
                    </Button.Background>
                </Button>
            </Grid>
            <Grid Grid.Row="1">

            </Grid>
            <Grid Grid.Row="2">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="1.4*"></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                    <ColumnDefinition ></ColumnDefinition>
                </Grid.ColumnDefinitions>
                <Button Style="{DynamicResource BUTTON_ELLIPSE}"
                        x:Name="OpenMySql"
                        Width="30"
                        Height="30"
                        ToolTip="导入MySql"
                        HorizontalAlignment="Left"
                        Margin="33,0,0,0" 
                        VerticalAlignment="Center" 
                        Click="OpenMySql_Click"/>
                <!--<Button Style="{DynamicResource BUTTON_ELLIPSE}"
                        x:Name="btn_post"
                        Width="30"
                        Height="30"
                        ToolTip="发送"
                        HorizontalAlignment="Left"
                        Margin="80,0,0,0" 
                        VerticalAlignment="Center" 
                        Click="btn_post_Click"/>-->
                <Button Grid.Column="1"
                        x:Name="btn_Save"
                        Style="{StaticResource  BUTTON_AGREE}" 
                        Content="导入并输出Excel"
                        Margin="0,8,0,0"
                        VerticalAlignment="Center"
                        HorizontalAlignment="Center" 
                        Width="130" 
                        Height="40"            
                        FontSize="15"
                        BorderBrush="{x:Null}" 
                        Click="btn_Save_Click"
                        Background="#FF36B5C1"/>

                <Button Grid.Column="2"
                        x:Name="btn_Cancel"
                        Style="{StaticResource  BUTTON_DISAGREE}" 
                        Content="取消" 
                        FontSize="15"
                        HorizontalAlignment="Center"
                        VerticalAlignment="Center"
                        Width="120" 
                        Height="40"
                        Margin="0,8,0,0" 
                        Click="btn_Close_Click"
                        Foreground="Gray"/>
            </Grid>
        </Grid>
    </Grid>
</Window>

 

WPF(Windows Presentation Foundation)是一个强大的框架,用于创建用户界面丰富的Windows应用程序。在WPF中,你可以轻松地定制包括按钮在内的UI元素的外观。对于美观的按钮样式,你可以利用XAML (XML-based language) 和ResourceDictionary来定义自定义模板。 以下是一个简单的例子展示如何创建一个带图标、文字和圆角的漂亮按钮样式: ```xml <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Button x:Key="MyAwesomeButton" Style="{StaticResource ButtonStyle}"> <StackPanel> <Image Source="icon.png" Margin="5"/> <TextBlock Text="点击我" FontSize="18" Margin="5,0,0,5"/> </StackPanel> </Button> <Style x:Key="ButtonStyle" TargetType="Button"> <Setter Property="Background" Value="#3498DB"/> <Setter Property="Foreground" Value="White"/> <Setter Property="BorderBrush" Value="Transparent"/> <Setter Property="BorderThickness" Value="1"/> <Setter Property="Cursor" Value="HandPointer"/> <Setter Property="Padding" Value="10"/> <Setter Property="Template"> <Setter.Value> <ControlTemplate> <Border CornerRadius="5"> <ContentPresenter/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </ResourceDictionary> ``` 在这个例子中,`ButtonStyle`设置了按钮的基本属性,如背景颜色、前景色等,并通过`ControlTemplate`设置了圆角边框效果。将这个`ButtonStyle`应用到`MyAwesomeButton`上即可获得预设的美观样式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值