首页 > 其他分享 >WPF使用静态属性制作按钮样式

WPF使用静态属性制作按钮样式

时间:2024-05-21 11:07:38浏览次数:17  
标签:ConvertFromString 静态 ColorConverter SolidColorBrush Color 按钮 new WPF public

<Window.Resources>
<!-- 定义样式 -->
<Style x:Key="CustomButtonStyle" TargetType="Button">
<Setter Property="Tag" Value="{x:Static local:ButtonColorSets.Button1Colors}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<Border x:Name="border" Background="{TemplateBinding Background}" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
<ControlTemplate.Triggers>
<!-- 正常状态 -->
<Trigger Property="IsPressed" Value="False">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.NormalColor}"/>
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.NormalColor}"/>
</Trigger>

<!-- 按下状态 -->
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.PressedColor}"/>
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.PressedColor}"/>
</Trigger>
<!-- 鼠标悬停状态 -->
<Trigger Property="IsMouseOver" Value="True">
<!--<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.MouseOverColor}"/>-->
</Trigger>
<!-- 禁用状态 -->
<Trigger Property="IsEnabled" Value="False">
<Setter TargetName="border" Property="Background" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.DisabledColor}"/>
<Setter TargetName="border" Property="BorderBrush" Value="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tag.DisabledColor}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
<!-- 设置默认 Tag -->
<Style.Triggers>
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Tag}" Value="{x:Null}">
<Setter Property="Tag" Value="{x:Static local:ButtonColorSets.Button1Colors}"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel Background="#fff">
<!-- 不设置 Tag 的按钮,将使用默认颜色 -->
<Button Content="Button 1" Width="100" Height="30" Style="{StaticResource CustomButtonStyle}" HorizontalAlignment="Left" />

<!-- 设置 Tag 为第二个颜色 -->
<Button Content="Button 2" Width="100" Height="30" Style="{StaticResource CustomButtonStyle}"
Tag="{x:Static local:ButtonColorSets.Button2Colors}" HorizontalAlignment="Left" />
<Button IsEnabled="True" Content="Button 3" Width="100" Height="30" Style="{StaticResource CustomButtonStyle}"
Tag="{x:Static local:ButtonColorSets.Button3Colors}" HorizontalAlignment="Left" />
</StackPanel>

public class ButtonColors
{
public Brush NormalColor { get; set; }
public Brush PressedColor { get; set; }
public Brush MouseOverColor { get; set; }
public Brush DisabledColor { get; set; }
}

public static class ButtonColorSets
{
public static ButtonColors Button1Colors { get; } = new ButtonColors
{
NormalColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#111")),
PressedColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#222")),
MouseOverColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#333")),
DisabledColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#444"))
};

public static ButtonColors Button2Colors { get; } = new ButtonColors
{
NormalColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#555")),
PressedColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#666")),
MouseOverColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#777")),
DisabledColor = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#888"))
};
public static ButtonColors Button3Colors { get; } = new ButtonColors
{
NormalColor = Brushes.Red,
PressedColor = Brushes.Pink,
MouseOverColor = Brushes.SkyBlue,
DisabledColor = Brushes.Green
};
}

标签:ConvertFromString,静态,ColorConverter,SolidColorBrush,Color,按钮,new,WPF,public
From: https://www.cnblogs.com/QJZY/p/18203555

相关文章

  • 防止页面按钮多次点击
    onlineAssessment(item){letelderInfoMation=JSON.stringify(item)console.log('this.isNavigating',this.isNavigating)if(this.isNavigating)return;//如果正在跳转,则直接返回不执行后续操作this......
  • WPF mvvm find element by name
    Copyfrom https://stackoverflow.com/questions/636383/how-can-i-find-wpf-controls-by-name-or-type //xaml<Windowx:Class="WpfApp102.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmln......
  • .htaccess伪静态规则
    Tips:当你看到这个提示的时候,说明当前的文章是由原emlog博客系统搬迁至此的,文章发布时间已过于久远,编排和内容不一定完整,还请谅解`.htaccess伪静态规则日期:2017-12-4阿珏折腾代码浏览:1701次评论:1条Apache的mod_rewrite是比较强大的,在进行网站建设时,可以通过这个模......
  • 【.NET项目分享】免费开源的静态博客生成工具EasyBlog,5分钟拥有自己的博客
    EasyBlog说明本博客系统通过构建工具生成纯静态的博客网站,借助GitHubPages,你可以在5分钟内免费拥有个人博客。它具有以下特点生成纯静态网站,访问速度极快使用markdown格式来编写博客内容基于git代码管理来存储你的博客使用CI工具来自动化部署你的博客站点效果展示:NilTo......
  • 【C#】WPF字典资源设置Button控件样式
    <ResourceDictionaryxmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"><Stylex:Key="BtnInfoStyle"TargetType="Button">......
  • GoF之代理模式(静态代理+动态代理(JDK动态代理+CGLIB动态代理带有一步一步详细步骤))
    1.GoF之代理模式(静态代理+动态代理(JDK动态代理+CGLIB动态代理带有一步一步详细步骤))@目录1.GoF之代理模式(静态代理+动态代理(JDK动态代理+CGLIB动态代理带有一步一步详细步骤))每博一文案2.代理模式的理解3.静态代理4.动态代理4.1JDK动态代理4.1.1JDK动态代理中(获取到目......
  • 自研WPF插件系统(沙箱运行及热插拔)
    前言插件化的需求主要源于对软件架构灵活性的追求,特别是在开发大型、复杂或需要不断更新的软件系统时,插件化可以提高软件系统的可扩展性、可定制性、隔离性、安全性、可维护性、模块化、易于升级和更新以及支持第三方开发等方面的能力,从而满足不断变化的业务需求和技术挑战。一......
  • centos 7 配置静态 ip
    在centos7中,静态ip由文件/etc/sysconfig/network-scripts/ifcfg-ens32指定,默认情况下,内容如下:TYPE=EthernetPROXY_METHOD=noneBROWSER_ONLY=noBOOTPROTO=dhcpDEFROUTE=yesIPV4_FAILURE_FATAL=noIPV6INIT=yesIPV6_AUTOCONF=yesIPV6_DEFROUTE=yesIPV6_FAILURE_......
  • CentOS配置静态IP
    1.查看虚拟机使用的网络连接方式2.查看虚拟机的网络连接方式IP3.1.CentOS6.8之前IP配置方法打开系统输入setup,进入文本模式设置工具进入网络配置模式进入网络配置模式过后在进入设置配置,选项eth0网卡进入配置静态IP关闭DHCP,在静态IP,子网掩码和默认网关输入自己需要设定的......
  • 01_WPF+Prism登录之PasswordBox的Binding
    #region登录信息///<summary>///密码///</summary>privatestring_Pwd;///<summary>///密码///</summary>publicstringPwd{get{return_Pwd;}......