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

带有图片的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. Invalid revision: 3.18.1-g262b901-dirty CMake Error: CMake was unable to find a build program corresponding to "Ninja".

    一次在GitHub上找到的项目,本想编译运行下,但报如下的问题 错误一 Invalid revision: 3.18.1-g262b901-dirty 解决办法: 这是因为版本不对应,可在local. ...

  2. 前端实现预览PDF

    下载包 npm install react-pdf 我使用的是react-pdf@5.7.2版本 以下例子使用的是react创建的项目 直接上代码=>cv可用,保证高效 1.新增依赖 yarn ...

  3. 接口签名规则及Java代码demo实现

    接口签名规则及Java代码demo实现 签名规则 签名生成的通用步骤如下: 第一步,设所有发送或者接收到的数据为集合M,将集合M内非空参数值的参数按照参数名ASCII码从小到大排序(字典序),使用UR ...

  4. Zabbix---数据库表分区

    1) 查询zabbix数据库中各种表存储的大小和行数: mysql> select table_name, (data_length + index_length)/1024/1024 as t ...

  5. linux 4.19 ip重组

    IP重组 ip重组这部分 4.19内核与3.10内核有些差别,4.9.134以后内核中不使用低水位和工作队列了,同时使用了rhashtable 替代了 hash bucket的概念,在3.10内核中使 ...

  6. python3 安装pymssql失败 pip3 install pymssql

    python3 安装pymssql失败 报错信息: AttributeError: module 'platform' has no attribute 'linux_distribution' 解决 ...

  7. Mysql的Innodb和MyISAM引擎的区别

    区别项 Innodb MyISAM  事务  支持  不支持 锁粒度  行锁,适合高并发 表锁,不适合高并发  是否默认  默认  非默认  支持外键  支持外键  不支持  适合场景  读写均衡,写 ...

  8. Maven pom.xml文件

    pom.xml 版本依赖 <!--编译器依赖--> <properties> <project.build.sourceEncoding>UTF-8</pro ...

  9. Django查询特定条件的数据并插入其他表格模型

    要将特定 wk_nu 值对应的数据批量插入到 MPS005D3Model 中,你可以执行以下步骤: 确定要插入的 wk_nu 值. 获取与该 wk_nu 相关的数据. 将获取的数据逐一创建为 MPS0 ...

  10. [oeasy]python0082_[趣味拓展]控制序列_清屏_控制输出位置_2J

    光标位置 回忆上次内容 上次了解了键盘演化的过程 ESC 从 组合键 到 独立按键   ​   添加图片注释,不超过 140 字(可选)   ESC的作用 是 进入 控制序列 配置 控制信息 控制信息 ...