WPF 基础控件之CheckBox样式
WPF開發者QQ群:340500857
?? ??? 由于微信群人數太多入群請添加小編微信號
?yanjinhuawechat 或 W_Feng_aiQ?邀請入群
?需備注WPF開發者?
?PS:有更好的方式歡迎推薦。
支持Nuget
Install-Package WPFDevelopers.Minimal -Version 3.0.0
01
—
代碼如下
一、創建?Styles.CheckBox.xaml?代碼如下。
<ResourceDictionary?xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:sys="clr-namespace:System;assembly=mscorlib"><ResourceDictionary.MergedDictionaries><ResourceDictionary?Source="../Themes/Basic/ControlBasic.xaml"/><ResourceDictionary?Source="../Themes/Basic/Animations.xaml"/></ResourceDictionary.MergedDictionaries><Style?TargetType="{x:Type?CheckBox}"?BasedOn="{StaticResource?ControlBasicStyle}"><Setter?Property="FocusVisualStyle"?Value="{x:Null}"?/><Setter?Property="Cursor"?Value="Hand"/><Setter?Property="Template"><Setter.Value><ControlTemplate?TargetType="{x:Type?CheckBox}"><BulletDecorator?Background="Transparent"><BulletDecorator.Bullet><Border?CornerRadius="{Binding?ElementName=PART_Border,Path=CornerRadius}"Background="{Binding?ElementName=PART_Border,Path=Background}"><Border?x:Name="PART_Border"?Width="16"?Height="16"CornerRadius="3"BorderThickness="1"BorderBrush="{DynamicResource?BaseSolidColorBrush}"Background="{DynamicResource?WhiteSolidColorBrush}"><Grid><Path?x:Name="PART_CheckMark"?SnapsToDevicePixels="False"?VerticalAlignment="Center"?HorizontalAlignment="Center"Data="{StaticResource?PathCheckMark}"RenderTransformOrigin=".5,.5"Stretch="Fill"?Fill="{DynamicResource?WhiteSolidColorBrush}"><Path.RenderTransform><ScaleTransform?ScaleX="0"?ScaleY="0"/></Path.RenderTransform></Path><Rectangle?Width="6"?Height="2"?VerticalAlignment="Center"?HorizontalAlignment="Center"x:Name="PART_InderminateMark"RenderTransformOrigin=".5,.5"Fill="{DynamicResource?PrimaryNormalSolidColorBrush}"><Rectangle.RenderTransform><ScaleTransform?ScaleX="0"/></Rectangle.RenderTransform></Rectangle></Grid></Border></Border></BulletDecorator.Bullet><ContentPresenter?x:Name="PART_ContentPresenter"Focusable="False"HorizontalAlignment="{TemplateBinding?HorizontalContentAlignment}"?Margin="2,0,0,0"?RecognizesAccessKey="True"?SnapsToDevicePixels="{TemplateBinding?SnapsToDevicePixels}"?VerticalAlignment="{TemplateBinding?VerticalContentAlignment}"TextElement.Foreground="{DynamicResource?PrimaryTextSolidColorBrush}"></ContentPresenter><VisualStateManager.VisualStateGroups><VisualStateGroup?x:Name="CheckStates"><VisualState?x:Name="Checked"><Storyboard><DoubleAnimation?Storyboard.TargetName="PART_CheckMark"Storyboard.TargetProperty="(Path.RenderTransform).(ScaleTransform.ScaleX)"?To=".7"?Duration="00:00:.2"EasingFunction="{StaticResource?ExponentialEaseOut}"/><DoubleAnimation?Storyboard.TargetName="PART_CheckMark"Storyboard.TargetProperty="(Path.RenderTransform).(ScaleTransform.ScaleY)"?To=".5"?Duration="00:00:.2"EasingFunction="{StaticResource?ExponentialEaseOut}"/></Storyboard></VisualState><VisualState?x:Name="Unchecked"?/><VisualState?x:Name="Indeterminate"/></VisualStateGroup></VisualStateManager.VisualStateGroups></BulletDecorator><ControlTemplate.Triggers><Trigger?Property="IsChecked"?Value="True"><Setter?Property="Background"?TargetName="PART_Border"?Value="{DynamicResource?PrimaryNormalSolidColorBrush}"/><Setter?Property="BorderBrush"?TargetName="PART_Border"?Value="{DynamicResource?PrimaryNormalSolidColorBrush}"/><Setter?Property="TextElement.Foreground"?TargetName="PART_ContentPresenter"?Value="{DynamicResource?PrimaryNormalSolidColorBrush}"/></Trigger><Trigger?Property="IsChecked"?Value="{x:Null}"><Trigger.EnterActions><BeginStoryboard?x:Name="PART_BeginStoryboardIsCheckedNull"><Storyboard><DoubleAnimation?Storyboard.TargetName="PART_InderminateMark"Storyboard.TargetProperty="(Path.RenderTransform).(ScaleTransform.ScaleX)"?To="1"?Duration="00:00:.2"EasingFunction="{StaticResource?ExponentialEaseOut}"/></Storyboard></BeginStoryboard></Trigger.EnterActions><Trigger.ExitActions><RemoveStoryboard?BeginStoryboardName="PART_BeginStoryboardIsCheckedNull"></RemoveStoryboard></Trigger.ExitActions></Trigger><MultiTrigger><MultiTrigger.Conditions><Condition?Property="IsChecked"?Value="False"?/><Condition?Property="IsMouseOver"?Value="True"?/></MultiTrigger.Conditions><Setter?Property="BorderBrush"?TargetName="PART_Border"?Value="{DynamicResource?PrimaryMouseOverSolidColorBrush}"/></MultiTrigger></ControlTemplate.Triggers></ControlTemplate></Setter.Value></Setter></Style></ResourceDictionary>二、使用?Styles.CheckBox.xaml?代碼如下。
<TextBlock?Text="Checkbox"?FontSize="20"?Margin="0,20,0,0"/><WrapPanel?Margin="0,10"><CheckBox?Content="Option?A"/><CheckBox?Content="Option?B"?Margin="10,0"?IsChecked="True"/><CheckBox?Content="Option?C"?IsChecked="{x:Null}"/><CheckBox?Content="Option?D"?Margin="10,0"?IsEnabled="False"/></WrapPanel>02
—
效果預覽
鳴謝素材提供者 -?element
源碼地址如下
Github:https://github.com/WPFDevelopersOrg
Gitee:https://gitee.com/WPFDevelopersOrg
https://gitee.com/WPFDevelopersOrg/WPFDevelopers.Minimal
https://github.com/WPFDevelopersOrg/WPFDevelopers.Minimal
WPF開發者QQ群:?340500857?
Github:https://github.com/WPFDevelopersOrg
出處:https://www.cnblogs.com/yanjinhua
版權:本作品采用「署名-非商業性使用-相同方式共享 4.0 國際」許可協議進行許可。
轉載請著名作者 出處 https://github.com/WPFDevelopersOrg
掃一掃關注我們,
更多知識早知道!
點擊閱讀原文可跳轉至源代碼
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的WPF 基础控件之CheckBox样式的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: Windows 11 上大招!正式支持安
- 下一篇: C#使用NPOI导出Excel文件
