c# WPF中CheckBox样式的使用总结
作者:Hello 寻梦者! 发布时间:2023-07-17 15:44:46
标签:c#,WPF,CheckBox样式
背景
很多时候我们使用WPF开发界面的时候经常会用到各种空间,很多时候我们需要去自定义控件的样式来替换默认的样式,今天通过两个方法来替换WPF中的CheckBox样式,透过这两个例子我们可以掌握基本的WPF样式的开发如何定义ControlTemplate以及使用附加属性来为我们的控件增加新的样式。
常规使用
我们在使用CheckBox的时候,原始的样式有时不能满足我们的需求,这是我们就需要更改其模板,比如我们常用的一种,在播放器中“播放”、“暂停”按钮,其实这也是一种CheckBox,只不过我们只是修改了其相关的模板罢了,下面贴出相关代码:
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="Focusable" Value="False" />
<Setter Property="IsTabStop" Value="False" />
<!--把OverridesDefaultStyle设置为True,表示这个控件不使用当前Themes的任何属性。-->
<Setter Property="OverridesDefaultStyle" Value="True" />
<Style.Triggers>
<Trigger Property="IsChecked" Value="True">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid Background="Transparent">
<Image Source="/EarthSimulation;component/Images/按钮-播放.png"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
<Trigger Property="IsChecked" Value="False">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Grid Background="Transparent">
<Image Source="/EarthSimulation;component/Images/按钮-暂停.png"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
进阶用法
上面的使用较为简单,下面我们通过一个更加复杂一些的例子来增加对自定义控件模板的理解,我们先来看看我们定义的样式。
<Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}">
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="BorderBrush" Value="Transparent"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type CheckBox}">
<Border x:Name="templateRoot" Background="{TemplateBinding Background}" >
<Grid SnapsToDevicePixels="True">
<Grid.Resources>
<PathGeometry x:Key="geometryCheck" Figures="M540.5696 102.4c-225.83296 0-409.6 183.74656-409.6 409.6s183.76704 409.6 409.6 409.6c225.87392 0 409.6-183.74656 409.6-409.6S766.44352 102.4 540.5696 102.4zM721.16224 468.48l-175.37024 175.39072c-12.20608 12.1856-28.20096 18.28864-44.19584 18.28864-15.95392 0-31.96928-6.10304-44.15488-18.28864l-97.44384-97.44384c-24.39168-24.39168-24.39168-63.93856 0-88.33024 24.39168-24.41216 63.91808-24.41216 88.35072 0l53.248 53.248 131.23584-131.21536c24.35072-24.3712 63.95904-24.3712 88.33024 0C745.55392 404.52096 745.55392 444.08832 721.16224 468.48z"/>
</Grid.Resources>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="20"/>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="30"/>
</Grid.ColumnDefinitions>
<Border>
<Path Width="18" Height="18" Stretch="Uniform"
Data="{Binding (local_ctrl:GeometryAP.IconGeometry), RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type CheckBox}}}"
Fill="{Binding (local_ctrl:GeometryAP.IconFill), RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type CheckBox}}}"/>
</Border>
<ContentPresenter x:Name="contentPresenter" Grid.Column="1" Focusable="False"
Margin="{TemplateBinding Padding}"
RecognizesAccessKey="True"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
HorizontalAlignment="Left"
VerticalAlignment="Center"/>
<Border x:Name="checkBoxBorder"
Grid.Column="2"
Margin="1"
BorderBrush="Transparent"
BorderThickness="1"
Background="Transparent"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<Grid x:Name="markGrid">
<Path x:Name="optionMark" Width="20" Height="20"
Stretch="Uniform"
Data="{StaticResource geometryCheck}"
Fill="LightSeaGreen" Margin="1" Opacity="0"/>
</Grid>
</Border>
</Grid>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="HasContent" Value="true">
<Setter Property="Padding" Value="4,-1,0,0"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Background" Value="#22222222"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Opacity" Value=".3"/>
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Cursor" Value="Hand"/>
<Setter Property="Background" Value="#22333333"/>
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Opacity" TargetName="optionMark" Value="1"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
后面我们再来看看,我们使用CheckBox的地方。
<CheckBox Grid.Row="1" Grid.Column="1"
IsChecked="{Binding VM.IsHorizontal,ElementName=_this}" Content="Horizontal"
Style="{StaticResource CheckBoxStyle}"
local_ctrl:GeometryAP.IconGeometry="{StaticResource geometryDirection}"
local_ctrl:GeometryAP.IconFill="LightSeaGreen"/>
这个地方我们为CheckBox增加了两个附加属性IconGeometry、IconFill这样我们就能够将这两个附加属性绑定到CheckBox样式中的Path里面的Data和Fill依赖项属性上面,通过上面的过程我们就能够定义各种各样的CheckBox样式了,下面我们看看我们定义的这两个附加属性具体的代码。
public class GeometryAP : DependencyObject
{
public static PathGeometry GetIconGeometry(DependencyObject obj)
{
return (PathGeometry)obj.GetValue(IconGeometryProperty);
}
public static void SetIconGeometry(DependencyObject obj, PathGeometry value)
{
obj.SetValue(IconGeometryProperty, value);
}
public static Brush GetIconFill(DependencyObject obj)
{
return (Brush)obj.GetValue(IconFillProperty);
}
public static void SetIconFill(DependencyObject obj, Brush brush)
{
obj.SetValue(IconFillProperty, brush);
}
public static readonly DependencyProperty IconGeometryProperty = DependencyProperty.RegisterAttached("IconGeometry", typeof(PathGeometry), typeof(GeometryAP));
public static readonly DependencyProperty IconFillProperty = DependencyProperty.RegisterAttached("IconFill", typeof(Brush), typeof(GeometryAP), new PropertyMetadata(Brushes.Transparent));
}
样式欣赏
来源:https://www.cnblogs.com/seekdream/p/4604380.html


猜你喜欢
- 云计算、大数据地快速发展催生了不少热门的应用及工具。作为老牌语言Java,其生态圈也出来了一些有关云服务、监控、文档分享方面的工具。本文总结
- 在 Effecitve Java 一书的第 48 条中提到了双重检查模式,并指出这种模式在 Java 中通常并不适用。该模式的结构如下所示:
- Mapper类中存在名称相同的方法重载报错项目场景官网项目开发问题描述Mapper类中存在名称相同的方法重载报错为了省事,直接在mapper
- Hutool Java工具类库_ExcelUtil依赖<!--Hutool Java工具包--> &l
- 意图:想将项目用到的两个dll库文件(CryptEnDe.dll和ICSharpCode.SharpZipLib.dll)一同编译进exe中
- 关于ListBoxListBox是WinForm中的列表控件,它提供了一个项目列表(一组数据项),用户可以选择一个或者多个条目,当列表项目过
- 单例模式的介绍说到单例模式,大家第一反应应该就是——什么是单例模式?,从“单例”字面意思上理解为——一个类只有一个实例,所以单例模式也就是保
- spring task和线程池的研究最近因工作需求,研究了一下spring task定时任务,和线程池,有了一定收获,记录一下涉及如下内容1
- 一、前言1、热更新代码的场景(1)当线上服务器出现问题时,有些时候现有的手段不足以发现问题所在,可能需要追加打印日志或者增加一些调试代码,如
- java 中设计模式(值对象)的实例详解应用场景:在Java开发时,需要来回交换大量的数据,比如要为方法传入参数,也要获取方法的返回值,该如
- 本文实例为大家分享了Android Studio实现简易计算器App的具体代码,供大家参考,具体内容如下效果演示布局文件<?xml v
- IDEA修改idea64.exe.vmoptions文件及解决coding卡顿用idea软件同时打开多个项目时,过个几天不关闭,就慢慢陷入卡
- 一、增删改1、增加<!-- 添加用户--><insert id="saveUser" paramete
- 为了保持类型的安全性,默认情况下 C# 是不支持指针的,但是如果使用 unsafe 关键字来修饰类或类中的成员,这样的类或类中成员就会被视为
- 先创建一个CacheHelper.cs类,代码如下:using System;using System.Web;using System.C
- 首先我们如果要使用Java中存在的包,可以程序中使用import语句导入包。包说通俗点就是一个文件夹,为了方便管理。在程序中声明包的语法:p
- 本文实例为大家分享了Android文本视图TextView实现聊天室的具体代码,供大家参考,具体内容如下Math.random()生成随机数
- 这篇文章主要介绍了Spring Boot项目中定制 * 的方法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价
- Jar包加密工具 ClassFinal介绍ClassFinal 是一款 java class 文件安全加密工具,支持直接加密jar包或war
- 使用Kotlin的Lambda表达式,我们可以抛弃回调接口的使用。只需设置希望后面会被调用的函数即可。示例如下新建一个Kotlin类clas