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 ...
随机推荐
- Vue2复习
Vue2 插值.指令.动态属性.表达式.v-html 插值:{{ data }} 指令 & 动态属性:例子(:id="xxx") 表达式:可以用于赋值,写在{{}}里面 v ...
- react的类组件的ts写法
react的类组件的ts写法,声明的变量,props和state的写法 import React, { PureComponent } from 'react'; interface Iprops { ...
- 部署jar项目服务命令
部署jar项目服务命令首先使用jenkins打包jar history | grep java 查看ps aux | grep 服务关键字关闭进程,否则启动的时候报错:java.net.BindExc ...
- #PowerBi 1分钟学会,用PowerBi获取数据库最近90天的数据(DATE_SUB)
在powerbi报表中,我们往往会对数据源进行日常刷新,powerbi链接了数据库的情况下,根据日期灵活取数是我们必须掌握的一个技能. 在本文中,我们将介绍如何使用 SQL 的 DATE_SUB 函数 ...
- uniapp ios推送 离线推送收不到消息
突然之间收不到离线推送消息了,角标也不显示了. 查了很长时间发现是ios的推送证书过期了. 我用的是appuploader登陆上以后在证书管理中新创建证书就可以了.
- Linux信号量
查看信号量 [root@localhost ~]# kill -l 1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP 6) SIGABRT 7) ...
- Linux驱动中的等待队列与休眠
Linux驱动中的等待队列与休眠 原文:https://blog.csdn.net/mengluoxixiang/article/details/46239523?spm=1001.2014.3001 ...
- ZYNQ:使用 PetaLinux 构建Linux项目
参考文档:ug1144-petalinux-tools-reference-guide.pdf 环境安装 tofrodos iproute2 gawk gcc g++ git make net-too ...
- Xilinx SDK 开发Linux APP
Xilinx SDK 开发Linux APP 步骤 配置环境变量 将工具链需要的程序的所在目录添加到 系统环境变量中,例如: D:\Xilinx_201803\SDK\2018.3\gnu\micro ...
- SpringBoot彩蛋之定制启动画面
写在前面 在日常开发中,我们经常会看到各种各样的启动画面.例如以下几种 ① spring项目启动画面 ② mybatisplus启动画面 ③若依项目启动画面 还有很多各式各样好看的启动画面,那么怎么定 ...