【WPF】XAML实现按钮背景图片的点击切换
原因:要做一组搜索结果的排序按钮(类似一组RadioButton),效果像下图这样。想法是使用原生的按钮控件,将文字左对齐,整个按钮背景是一张图片,通过样式Trigger控制字体变色、背景图切换。
需求:RadioButton开关按钮,点击后切换自身按钮的背景图片。
MyRadioButton.xaml
<ResourceDictionary x:Class="HomeDecorationPSD.Presentation.Style.MyRadioButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:HomeDecorationPSD.Presentation.Style"
mc:Ignorable="d">
<ResourceDictionary.MergedDictionaries>
<!-- 引入颜色字符串 -->
<ResourceDictionary Source="/Presentation/Resources/ColorResources.xaml" />
</ResourceDictionary.MergedDictionaries>
<Style x:Key="myRadioButton" TargetType="{x:Type RadioButton}">
<Setter Property="BorderThickness" Value="0"></Setter>
<Setter Property="Width" Value="80"></Setter>
<Setter Property="Height" Value="30"></Setter>
<Setter Property="Foreground" Value="{StaticResource LightGreyColor}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type RadioButton}">
<Grid x:Name="grid" VerticalAlignment="Center">
<Border x:Name="border" BorderThickness="1" BorderBrush="{StaticResource LightGreyColor}" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}"
HorizontalAlignment="Center" Background="#E9E9E9" Padding="5,0,0,0">
<ContentPresenter Content="{TemplateBinding Content}" VerticalAlignment="Center" HorizontalAlignment="Left" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
</Grid>
<!-- 触发器:设置字体的颜色 -->
<ControlTemplate.Triggers>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Foreground" Value="{StaticResource SoftRedColor}"/> <!-- 被引入的颜色字符串 -->
<Setter TargetName="border" Property="Background">
<Setter.Value>
<ImageBrush ImageSource="/HomeDecorationPSD;component/Presentation/Resources/Images/down_arrow_selected.png" Stretch="Fill"/>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="false">
<Setter Property="Foreground" Value="{StaticResource LightGreyColor}"/>
<Setter TargetName="border" Property="Background"> <!-- 必须指明TargetName -->
<Setter.Value>
<ImageBrush ImageSource="/HomeDecorationPSD;component/Presentation/Resources/Images/down_arrow.png" Stretch="Fill"/>
</Setter.Value>
</Setter>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
坑点:
- ControlTemplate必须使用一个Border包裹。
- 在Trigger中给按钮设置文字颜色时,使用Foreground不需要指明TargetName,但是给背景设置图片时,必须要指明TargetName,否则无效果。(非常坑爹,运行无报错,能看到文字变色,不能看到背景图)
【WPF】XAML实现按钮背景图片的点击切换的更多相关文章
- currentBackgroundImage:获取按钮背景图片
NSData *imagedata1=UIImagePNGRepresentation(btn.currentBackgroundImage);//按钮背景图片转NSData NSData *imag ...
- 拉伸按钮背景图片:stretchableImageWithLeftCapWidth:
// 1. 拉伸按钮背景图片 // 1) 登录按钮 UIImage *loginImage = [UIImage imageNamed:@"LoginGreenBigBtn"]; ...
- 设置按钮背景图片(HTML-CSS)
很多人提交表单时都喜欢用一个图片来作为提交按钮,大多数人可能用JS去操作表单的提交,即当用户点击这个图片时响应一个JS来提交表单.其实还有一种方法,就是直接设置SUBMIT按钮的图片背景.设置它的图片 ...
- VUE2.0 饿了吗视频学习笔记(六):定位问题、文字显示、模糊背景图片、点击事件
一.定位问题按照视频写代码时,发现元素“5个“”定位不对,如下图 正常位置为 还以为是哪里写错了,仔细研究了下,需要在父div上加relative. position:relative/absolut ...
- wpf 控件添加背景图片
方法一,xaml中: <控件> <控件.Background> <ImageBrush ImageSource="/WpfApplication1;compon ...
- WPF中Button的背景图片,实现禁止IsMouseOver时显示默认
<Button x:Name="btnPickUpNum" Click="PickUpNum_OnClick" Grid.Row="1" ...
- swift - UIButton按钮有图片是点击高亮 有灰色动画
取消 高亮的 动画 btn.adjustsImageWhenHighlighted = false btn.layer.removeAllAnimations()
- ListView 点击某一项换背景图片
1. layout_search_list_item.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/r ...
- iOS开发之--如何修改TabBarItem的title的字体和颜色/BarButtonItem的title的字体大小和颜色/添加背景图片,并添加点击方法
在进行项目的过程中,我们往往会遇到各种各样的自定义颜色和字体,下面提供一种修改系统自带的TabBarItem的字体和颜色的方法,希望能帮到大家: [[UITabBarItem appearance] ...
随机推荐
- Spring框架文档与API(4.3.6版本)
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/ Table of Contents I ...
- HDUOJ----4502吉哥系列故事——临时工计划
吉哥系列故事——临时工计划 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Tot ...
- Python 的 Matplotlib 画图库
Matplotlib安装 NumPy库方便数值运算,但枯燥的数据并不利于人们的直观理解. 数据需要可视化. Matplotlib:一个数据可视化函数库 使用前需要安装 利用Python自带 ...
- 【LeetCode】130. Surrounded Regions (2 solutions)
Surrounded Regions Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A ...
- linux服务器的Gzip文件压缩方法[转]
一.gzip介绍 gzip是GNU zip的缩写,它是一个GNU自由软件的文件压缩程序,也经常用来表示gzip这种文件格式.软件的作者是Jean-loup Gailly和Mark Adler.1992 ...
- pandas 筛选指定行或者列的数据
pandas主要的两个数据结构是:series(相当于一行或一列数据结构和DataFrame(相当于多行多列的一个表格数据机构). 原文:https://www.cnblogs.com/gangand ...
- unity5 Orthographic模式相机视截体尺寸计算
一,通过编辑器中数值计算. 如图,相机为Orthographic模式,其camera size为5.57,是什么含义呢,经过测量,发现视图中视截体的高度是5.57x2. 那么视截体的宽度怎么决定呢? ...
- FBX BlendShape/Morph动画解析
目前fbx 2015.1中支持三种变形器:skinDeformer,blendShapeDeformer,vertexCacheDeformer.定义在fbxdeformer.h中: enum EDe ...
- mysqldump使用大全
普通备份 C:\Program Files\MySQL\MySQL Server 5.6\bin>mysqldump -uroot -p test > f:\test_bak_1009.s ...
- GPS轨迹数据集免费下载资源整理
https://blog.csdn.net/liangyihuai/article/details/58335510