WPF Button自定义样式收集 带有图片的Button
此篇只是收集平时写过的样式~
带有图片的Button
为Button设定了一些附加属性,用于添加图片到Button。
比如初始化图片和点击后的图片
public static readonly DependencyProperty NormalImageProperty = DependencyProperty.RegisterAttached(
"NormalImage",
typeof(ImageSource),
typeof(ButtonAttachments),
new PropertyMetadata(null)); public static ImageSource GetNormalImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(NormalImageProperty);
} public static void SetNormalImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(NormalImageProperty, value);
} public static readonly DependencyProperty PressedImageProperty = DependencyProperty.RegisterAttached(
"PressedImage",
typeof(ImageSource),
typeof(ButtonAttachments),
new PropertyMetadata(null)); public static ImageSource GetPressedImage(DependencyObject obj)
{
return (ImageSource)obj.GetValue(PressedImageProperty);
} public static void SetPressedImage(DependencyObject obj, ImageSource value)
{
obj.SetValue(PressedImageProperty, value);
}
图片的位置放置
public static readonly DependencyProperty ImagePositionProperty = DependencyProperty.RegisterAttached(
"ImagePosition",
typeof(PositionEnumType),
typeof(ButtonAttachments),
new PropertyMetadata(PositionEnumType.Right)); public static void SetImagePosition(DependencyObject element, PositionEnumType value)
{
element.SetValue(ImagePositionProperty, value);
} public static PositionEnumType GetImagePosition(DependencyObject element)
{
return (PositionEnumType) element.GetValue(ImagePositionProperty);
} public static readonly DependencyProperty ContentMarginProperty = DependencyProperty.RegisterAttached(
"ContentMargin",
typeof(Thickness),
typeof(ButtonAttachments),
new PropertyMetadata(default(Thickness))); public static void SetContentMargin(DependencyObject element, object value)
{
element.SetValue(ContentMarginProperty, value);
} public static Thickness GetContentMargin(DependencyObject element)
{
return (Thickness)element.GetValue(ContentMarginProperty);
}
接下来是Style的更改。Left表示图片在左边~
<DataTrigger Binding="{Binding Path=(res:ButtonAttachments.ImagePosition),RelativeSource={RelativeSource Self},Mode=OneWay}" Value="Left">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<Grid x:Name="MainGrid">
<Border x:Name="BorderBg"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
CornerRadius="{Binding Path=(res:ButtonAttachments.CornerRadius),RelativeSource={RelativeSource TemplatedParent}}"/>
<StackPanel VerticalAlignment="{TemplateBinding VerticalAlignment}"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
Margin="{TemplateBinding Padding}"
Orientation="Horizontal"> <Image Name="Image"
Stretch="Uniform"
Margin="{Binding Path=(res:ButtonAttachments.ContentMargin), RelativeSource={RelativeSource TemplatedParent}}"
Width="{Binding Path=(res:ButtonAttachments.ImageWidth), RelativeSource={RelativeSource TemplatedParent}}"
Height="{Binding Path=(res:ButtonAttachments.ImageHeight), RelativeSource={RelativeSource TemplatedParent}}"
Source="{Binding Path=(res:ButtonAttachments.NormalImage), RelativeSource={RelativeSource TemplatedParent}}"/>
<ContentPresenter RecognizesAccessKey="True"
VerticalAlignment="Center"/>
</StackPanel>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Image" Property="Image.Source" Value="{Binding Path=(res:ButtonAttachments.PressedImage), RelativeSource={RelativeSource TemplatedParent}}" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</DataTrigger>
使用样例
<Button Style="{StaticResource Style.ImageButton}"
Width="50"
Height="50"
Content="123"
Background="Transparent"
attachment:ButtonAttachments.ContentMargin="0 10 0 0"
attachment:ButtonAttachments.CornerRadius="10"
attachment:ButtonAttachments.ImagePosition="Bottom"
attachment:ButtonAttachments.ImageWidth="18"
attachment:ButtonAttachments.ImageHeight="18"
attachment:ButtonAttachments.NormalImage="{StaticResource Image.HelpNormal}"
attachment:ButtonAttachments.PressedImage="{StaticResource Image.HelpPress}"/>
白嫖链接
https://github.com/yinghualuowu/SakuraStyle
WPF Button自定义样式收集 带有图片的Button的更多相关文章
- Android实现自定义带文字和图片的Button
Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...
- WPF CheckBox 自定义样式
WPF 自定义样式.CheckBox <Style x:Key="EmptyCheckBox" TargetType="CheckBox"> < ...
- 【Android】Android实现自定义带文字和图片的Button
在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最 ...
- WPF BasedOn 自定义样式 例:ComboBox 组合框
自定义样式 ComboBox 组合框 <Window.Resources> <Style x:Key="ComboBox01" TargetType=" ...
- WPF DataGrid自定义样式
微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...
- Wpf ScrollBar自定义样式
Wpf的ScrollBar可以分为六个区域:A.背景.B.向上按钮.C.向下的按钮.D.Track里面向上的按钮.E.Track里面向下的按钮.F.Track的Thumb 详情见下图 下面通过一个例子 ...
- 【再学WPF】自定义样式
1.添加"资源字典": 工程名称:WpfApp1 新建Styles文件夹:创建"Dictionary1.xaml"的文件: 2.编辑样式: <SolidC ...
- WPF DataGrid 自定义样式 MVVM 删除 查询
看到很多小伙伴在找Dategrid样式 就分享一个 ,有不好的地方 请指出 代码部分都加了注释 需要的可以自己修改为自己需要的样式 源码已经上传 地址: https://github.com/YC ...
- 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别
这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...
- Button 自定义图片,代码绘制样式,添加音效的方法
Button自己在xml文件中绑定监听器 <!-- 设定onclick属性,然后在activity中定义相应的方法 --> <!-- 通过xml布局中通过button的android ...
随机推荐
- springmvc组件说明
DispatcherServlet:前端控制器,也称为中央控制器,它是整个请求响应的控制中心,组件的调用由它统一调度. HandlerMapping:处理器映射器,它根据用户访问的 URL 映射到对应 ...
- idea设置jdk和设置文件编码格式utf-8
1.idea设置jdk 2.idea设置文件编码格式utf-8 create utf-8 files with NO BOM 不要更改,否则编译会出错误.
- HBCK2修复hbase2的常见场景
上一文章已经把HBCK2 怎么在小于hbase2.0.3版本的编译与用法介绍了,解决主要场景 查看hbase存在的问题 一.使用hbase hbck命令 hbase hbck命令是对hbase的元数据 ...
- Windows10(or windows11) Hyper-V 创建虚拟交换机后宿主机上传速度变特别慢的问题解决
问题 我在我的win11上启用了Hyper-v,装了个虚拟机跑了个CentOS7.6,为了让centos和宿主机通信在同个网段搞了个桥接网络,网络环境如下 然后我测试一个文件上传功能的时候发现网络上传 ...
- 【JavaScript】聊聊js中关于this的指向
前言 最近在看回JavaScript的面试题,this 指向问题是入坑前端必须了解的知识点,现在迎来了ES6+的时代,因为箭头函数的出现,所以感觉有必要对 this 问题梳理一下,所以刚好总结一下Ja ...
- 使用Jest和React Test library 进行React单元测试
React单元测试,就是把React 组件渲染出来,看看渲染出来的内容符不符合我们的预期.比如组件加载的时候有loading, 那就渲染组件,看看渲染出的内容中有没有loading. 再比如,ajax ...
- 拟合算法与matlab工具包实现
与插值问题不同,在拟合问题中不需要曲线一定经过给定的点.拟合问题的目标是寻求一个函数(曲线),使得该曲线在某种准则下与所有的数据点最为接近,即曲线拟合的最好(最小化损失函数) 目录 一.插值和拟合的区 ...
- 移动WEB开发之 -- 流式布局
浏览器现状 视口 视口标签 二倍图 手机端和pc端像素比例不一样 物理像素&物理像素比 背景缩放background-size 背景图片二倍图 移动端开发选择 移动端技术解决方案 特殊样式 常 ...
- 推荐几款个人喜欢的IDEA开发工具主题【更舒适的开发】
IDEA,全称 IntelliJ IDEA ,是Java语言的集成开发环境,IDEA在业界被公认为是最好的 java 开发工具之一,尤其在智能 代码助手.代码自动提示.重构.J2EE支持. Ant . ...
- Mysql密码安全策略修改
Mysql5.7默认有密码安全策略,密码安全级别要求比较高,在测试环境中使用起来不方便,本经验将介绍如何修改Mysql的密码安全策略,解决ERROR 1819错误. 1:首先使用root用户连接mys ...