C#按钮样式设置XMAL

统一按钮样式

<Window.Resources> <!--按钮样式统一设置,个别按钮单独定义样式的话则需要在定义按钮位置单独设置-->
    <Style TargetType="Button">
        <Setter Property="Background" Value="Red"/>
        <Setter Property="FontSize" Value="20"/>
        <Setter Property="Height" Value="50"/>
        <Setter Property="Width" Value="300"/>

    </Style>
</Window.Resources>

设置多个样式,并引用。

    <Window.Resources> <!--按钮样式统一设置,个别按钮单独定义样式的话则需要在定义按钮位置单独设置-->
        <Style x:Key="LoginStyle" TargetType="Button">
            <Setter Property="Background" Value="Blue"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="Width" Value="300"/>
        </Style>
        
        <Style x:Key="QuitStyle"  TargetType="Button">
            <Setter Property="Background" Value="Red"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Height" Value="50"/>
            <Setter Property="Width" Value="300"/>
        </Style>

    </Window.Resources>
    
    
    
    
    <StackPanel>
        <Button Style="{StaticResource LoginStyle}" Content="登陆" Width="200" Height="50" FontSize="20"/>
        <Button Style="{StaticResource QuitStyle}" Content="退出" Width="200" Height="50" FontSize="20"/>
    </StackPanel>

设置button的基础样式样式,然后继承基础样式,构建多个不同的样式

   <Window.Resources> 
        <Style TargetType="Button"> <!--设定一个基础的样式:属于全局应用-->
            <Setter Property="Background" Value="WhiteSmoke"/>
            <Setter Property="FontSize" Value="20"/>
            <Setter Property="Margin" Value="20,10,20,10"/>
        </Style>

        <!--继承基础样式的基础生成新的样式1-->
        <Style x:Key="LoginStyle" TargetType="Button" BasedOn = "{StaticResource {x:Type Button} }">
            <Setter Property="Background" Value="Green"/>
        </Style>

        <!--继承基础样式的基础生成新的样式2-->
        <Style x:Key="QuitStyle" TargetType="Button" BasedOn = "{StaticResource {x:Type Button} }">
            <Setter Property="Background" Value="Red"/>
        </Style>



    </Window.Resources>

    <StackPanel>
        <Button  Style="{StaticResource LoginStyle}" Content="登陆" Width="200" Height="50" FontSize="20"/>
        <Button  Style="{StaticResource QuitStyle}" Content="退出" Width="200" Height="50" FontSize="20"/>
    </StackPanel>

自定义按钮,并且设置按钮的触发事件

 <Button Content="自定义按钮" FontSize="35" Height="120" Width="200" Background="Aquamarine" >
     <Button.Template>
         <ControlTemplate TargetType="Button">
             <Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="CadetBlue" BorderThickness="4" CornerRadius="10" HorizontalAlignment="Center" VerticalAlignment="Center">
                 <ContentPresenter/>
             </Border>
             <!--设置按钮的触发-->
             <ControlTemplate.Triggers>

                 <Trigger Property="IsMouseOver" Value="True"><!--鼠标放在上方的触发方式-->
                     <Setter TargetName="border" Property="Background" Value="red"/>
                 </Trigger>

                 <Trigger Property="IsPressed" Value="True"><!--鼠标点击的触发方式-->
                     <Setter TargetName="border" Property="Background" Value="Green"/>
                 </Trigger>

             </ControlTemplate.Triggers>
             
         </ControlTemplate>
     </Button.Template>
 </Button>

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值