此篇只是收集平时写过的样式~

带有图片的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的更多相关文章

  1. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

  2. WPF CheckBox 自定义样式

    WPF 自定义样式.CheckBox <Style x:Key="EmptyCheckBox" TargetType="CheckBox"> < ...

  3. 【Android】Android实现自定义带文字和图片的Button

    在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就是利用系统自带的Button来实现,这种方式代码量最 ...

  4. WPF BasedOn 自定义样式 例:ComboBox 组合框

    自定义样式 ComboBox 组合框 <Window.Resources> <Style x:Key="ComboBox01" TargetType=" ...

  5. WPF DataGrid自定义样式

    微软的WPF DataGrid中有很多的属性和样式,你可以调整,以寻找合适的(如果你是一名设计师).下面,找到我的小抄造型的网格.它不是100%全面,但它可以让你走得很远,有一些非常有用的技巧和陷阱. ...

  6. Wpf ScrollBar自定义样式

    Wpf的ScrollBar可以分为六个区域:A.背景.B.向上按钮.C.向下的按钮.D.Track里面向上的按钮.E.Track里面向下的按钮.F.Track的Thumb 详情见下图 下面通过一个例子 ...

  7. 【再学WPF】自定义样式

    1.添加"资源字典": 工程名称:WpfApp1 新建Styles文件夹:创建"Dictionary1.xaml"的文件: 2.编辑样式: <SolidC ...

  8. WPF DataGrid 自定义样式 MVVM 删除 查询

    看到很多小伙伴在找Dategrid样式 就分享一个 ,有不好的地方 请指出 代码部分都加了注释  需要的可以自己修改为自己需要的样式 源码已经上传 地址:  https://github.com/YC ...

  9. 关于<button> 没写 type='button' 导致点击时提交以及<button>和<input type="button">的区别

    这是我的第一篇博客,如果写的不好,请见谅 这是一个关于button按钮一个小问题 最近刚开学跟着老师一起写代码,在模仿JAVA web程序设计(慕课版) P61页第三章 Ajax处理XML的代码中发现 ...

  10. Button 自定义图片,代码绘制样式,添加音效的方法

    Button自己在xml文件中绑定监听器 <!-- 设定onclick属性,然后在activity中定义相应的方法 --> <!-- 通过xml布局中通过button的android ...

随机推荐

  1. 苹果手机 ios 系统如何升级为鸿蒙HarmonyOS

    用苹果手机的朋友们注意了 根据最新的可靠消息,苹果手机升级为HarmonyOS,教程如下: 第一步 手机电量充足的情况下,将苹果手机连接至WIFI无线网络. 第二步 ...... [下一页]

  2. Mysql RC/RR隔离原理和区别 不可重复读和可重复读

    Mysql RC/RR隔离原理和区别 不可重复读和可重复读 mysql四种隔离级别:1.未提交读(READ UNCOMMITED)脏读2.已提交读 (READ COMMITED)简称(RC) 不可重复 ...

  3. MySQL Explain 关键字详解

    概述 explain 关键字可以模拟执行 sql 查询语句,输出执行计划,分析查询语句的执行性能 使用方式如下:explain + sql explain select * from t1 执行计划各 ...

  4. 淘宝二面:千万级数据中如何用Redis维护热点数据"?

    MySQL里有千万条数据,但是Redis中只存10万的数据,如何保证redis中的数据都是热点数据? 我是小宋, 一个只熬夜但不秃头的Java程序员.关注我,带你轻松过面试.提升简历亮点(14个dem ...

  5. EasyExcel 无法读取图片?用poi写了一个工具类

    在平时的开发中,经常要开发 Excel 的导入导出功能.一般使用 poi 或者 EasyExcel 开发,使用 poi 做 excel 比较复杂,大部分开发都会使用 EasyExcel 因为一行代码就 ...

  6. C# SM2算法 加密,解密,签名,验签

    最近时间在整SM2算法,在网上看到不少代码,基本都是使用BouncyCastle库,现在这个版本算比较好的拿来分享给大家. 首先引入包 Portable.BouncyCastle 完整代码见Gitee ...

  7. 【译】了解17.10 GA 中最新的 Git 工具特性

    我们相信提高开发和团队协作的生产力可以帮助您产生更好的软件解决方案.这就是为什么 Visual Studio 版本控制团队发布了新特性,简化了内部循环和代码审查体验.您将获得 AI 支持编写提交消息. ...

  8. CSDN 大规模抓取 GitHub 上的项目到 GitCode,伪造开发者主页引公愤

    事件起因 CSDN旗下的GitCode最近因为一种极其不道德的行为引起了开发者的广泛愤怒和抗议.CSDN在没有通知或征求开发者同意的情况下,悄悄地将大量GitHub上的开源项目搬运到了其自己的GitC ...

  9. (sql语句试题练习及 参考答案解题思路+个人解题思路)

    SQL字段说明及数据 ======================================================================= 一.部门表字段描述:dp_no 部 ...

  10. Java核心字符串String进阶

    字符串对象 字符串是对象,不是简单数据类型 封装在java.lang包,自动导入 创建字符串对象 常见创建一个字符串对象有下面2个方法 String str=new String("chen ...