WPF CHECKBOX STYLE

源自 http://www.wpfhelper.com/index.php/android-ui-for-wpf/23-modern-ui-for-wpf/android-ui-for-wpf/26-wpf-checkbox-style-inspired-by-android

CheckBoxStyleXAMLWPF

How create WPF CheckBox style and customize it. The CheckBox style is inspired by Android design.

 

 

 

CheckBox design:

Grid with relative size of columns and rows makes possible change CheckBox size using Height and Width . Or you can use Viewbox.

Design of CheckBox style:

CheckBox states:

a) checked, disable

b) checked

c) indeterminate, disable

d) indeterminate

e) unchecked

f) pressed

  • CheckBox style

    ?


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

    22

    23

    24

    25

    26

    27

    28

    29

    30

    31

    32

    33

    34

    35

    36

    37

    38

    39

    40

    41

    42

    43

    44

    45

    46

    47

    48

    49

    50

    51

    52

    53

    54

    55

    56

    57

    58

    59

    60

    61

    62

    63

    64

    65

    66

    67

    68

    69

    70

    71

    72

    73

    74

    75

    76

    77

    78

    79

    80

    81

    82

    83

    84

    85

    86

    87

    88

    89

    90

    91

    92

    93

    94

    95

    96

    97

    98

    99

    100

    101

    102

    103

    104

    105

    106

    107

    108

    109

    110

    111

    112

    113

    114

    115

    116

    117

    <Style x:Key="CheckBoxStyle" TargetType="{x:Type CheckBox}">

        <Setter Property="SnapsToDevicePixels" Value="true" />

        <Setter Property="OverridesDefaultStyle" Value="true" />

        <Setter Property="Height" Value="30" />

        <Setter Property="FocusVisualStyle" Value="{DynamicResource MyFocusVisualStyte}" />

        <Setter Property="Template">

            <Setter.Value>

                <ControlTemplate TargetType="{x:Type CheckBox}">

                    <BulletDecorator>

                        <BulletDecorator.Bullet>

                            <Grid Height="{TemplateBinding Height}" Width="{Binding RelativeSource={RelativeSource Self}, Path=Height, UpdateSourceTrigger=PropertyChanged}"

                                  MinHeight="30" MinWidth="30" ShowGridLines="False">

                                <Grid.ColumnDefinitions>

                                    <ColumnDefinition Width="4*" />

                                    <ColumnDefinition Width="1*" />

                                    <ColumnDefinition Width="1*" />

                                    <ColumnDefinition Width="4*" />

                                    <ColumnDefinition Width="1*" />

                                    <ColumnDefinition Width="1*" />

                                    <ColumnDefinition Width="2*" />

                                    <ColumnDefinition Width="2*" />

                                </Grid.ColumnDefinitions>

                                <Grid.RowDefinitions>

                                    <RowDefinition Height="3*" />

                                    <RowDefinition Height="1*" />

                                    <RowDefinition Height="1*" />

                                    <RowDefinition Height="1*" />

                                    <RowDefinition Height="4*" />

                                    <RowDefinition Height="1*" />

                                    <RowDefinition Height="1*" />

                                    <RowDefinition Height="4*" />

                                </Grid.RowDefinitions>

                                <Border Name="MainBorder"

                                        Grid.ColumnSpan="9" Grid.RowSpan="9"

                                        CornerRadius="4"

                                        BorderThickness="1"

                                        Background="Transparent" />

                                <Border Name="InnerBorder"

                                        Grid.Column="1" Grid.ColumnSpan="5"

                                        Grid.Row="2" Grid.RowSpan="5"

                                        BorderThickness="1"

                                        BorderBrush="#808080" />

                                <Path Name="InnerPath"

                                      Grid.Column="1" Grid.ColumnSpan="5"

                                      Grid.Row="2" Grid.RowSpan="5"

                                      Data="M31,5 L19.5,5 19.5,19.5 34.5,19.5 34.5,11.75"

                                      Stretch="Fill" Stroke="#808080"/>

                                <Path Name="CheckMark"

                                      Grid.Column="2" Grid.ColumnSpan="5"

                                      Grid.Row="1" Grid.RowSpan="5"

                                      Opacity="0"

                                      Data="M9.07743946676476E-09,4.31805768640244L4.68740335877841,8.86361158398516C4.68740335877841,8.86361158398516,16.3281249985376,-2.42451336648723,16.3281249985376,-2.42451336648723L14.0622100581796,-4.77304938341948 4.68740335877846,4.31805791992662 2.22656251699567,1.93164208562579z"

                                      Fill="#3babe3"

                                      Stretch="Fill"

                                      Stroke="#3babe3" />

                                <Path Name="InderminateMark"

                                      Grid.Column="3"

                                      Grid.Row="4"

                                      Data="M0,4 L1,5 5,1 4,0"

                                      Opacity="0"

                                      Stretch="Fill"

                                      StrokeThickness="0"

                                      Fill="#808080" />

                            </Grid>

                        </BulletDecorator.Bullet>

                        <VisualStateManager.VisualStateGroups>

                            <VisualStateGroup x:Name="CheckStates">

                                <VisualState x:Name="Checked">

                                    <Storyboard>

                                        <DoubleAnimation Storyboard.TargetProperty="Opacity"

                                                 Storyboard.TargetName="CheckMark" Duration="0:0:0.2" To="1" />

                                    </Storyboard>

                                </VisualState>

                                <VisualState x:Name="Unchecked" >

                                    <Storyboard>

                                        <DoubleAnimation Storyboard.TargetProperty="Opacity"

                                                 Storyboard.TargetName="CheckMark" Duration="0:0:0.2" To="0" />

                                    </Storyboard>

                                </VisualState>

                                <VisualState x:Name="Indeterminate">

                                    <Storyboard>

                                        <DoubleAnimation Storyboard.TargetProperty="Opacity"

                                                 Storyboard.TargetName="InderminateMark" Duration="0:0:0.2" To="1" />

                                    </Storyboard>

                                </VisualState>

                            </VisualStateGroup>

                        </VisualStateManager.VisualStateGroups>

                        <ContentPresenter Margin="4,0,4,0"

                            VerticalAlignment="Center"

                            HorizontalAlignment="Left"

                            RecognizesAccessKey="True" />

                    </BulletDecorator>

                    <ControlTemplate.Triggers>

                        <Trigger Property="IsChecked" Value="True">

                            <Setter TargetName="InnerBorder" Property="Visibility" Value="Collapsed" />

                        </Trigger>

                        <Trigger Property="IsPressed" Value="True">

                            <Setter TargetName="MainBorder" Property="Background" Value="#81d2eb" />

                        </Trigger>

                        <Trigger Property="IsEnabled" Value="False">

                            <Setter TargetName="CheckMark" Property="Fill" Value="#cccccc" />

                            <Setter TargetName="CheckMark" Property="Stroke" Value="#cccccc" />

                            <Setter TargetName="InnerPath" Property="Stroke" Value="#cccccc" />

                            <Setter TargetName="InderminateMark" Property="Fill" Value="#cccccc" />

                            <Setter TargetName="InnerBorder" Property="BorderBrush" Value="#cccccc" />

                        </Trigger>

                    </ControlTemplate.Triggers>

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

  • MyFocusVisualStyle

    ?


    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    <Style x:Key="MyFocusVisualStyte" >

        <Setter Property="Control.Template">

            <Setter.Value>

                <ControlTemplate>

                    <Rectangle x:Name="FocusStyle" StrokeDashArray="4 4" RadiusX="5" RadiusY="5" Fill="Transparent"

                               Stroke="#81d2eb" StrokeThickness="1" />

                </ControlTemplate>

            </Setter.Value>

        </Setter>

    </Style>

时间: 2024-10-06 16:06:17

WPF CHECKBOX STYLE的相关文章

WPF 样式(Style)初体验 (一) 作用域

刚刚接触WPF的开发,顿时对样式设计产生了兴趣.因为之前对CSS比较感兴趣,不难发现WPF的Style和CSS的模式很类似.下面我就根据自己初步的理解和CSS样式表对比做下总结,有理解不正确的地方还希望各位前辈指正. (1)全局样式控制 影响的是整个项目的样式,我们可以在App.xaml文件里定义全局的样式(这里就类似于我们在一个web的项目中添加一个全局的CSS文件,然后在每个页面引用CSS样式): <Application.Resources> <Style TargetType=&

WPF 中style文件的引用

原文:WPF 中style文件的引用 总结一下WPF中Style样式的引用方法: 一,内联样式: 直接设置控件的Height.Width.Foreground.HorizontalAlignment.VerticalAlignment等属性.以设置一个Botton控件的样式为例,如: 复制代码 <Grid x:Name="ContentPanel" > <Button Content="Button" Name="btnDemo"

WPF RadioButton &amp; CheckBox Style

<Style TargetType="CheckBox"> <Setter Property="Template"> <Setter.Value> <ControlTemplate TargetType="CheckBox"> <Border x:Name="bd" BorderBrush="Gray" BorderThickness="1&q

WPF CheckBox 样式

<Style x:Key="FocusVisual"> <Setter Property="Control.Template"> <Setter.Value> <ControlTemplate> <Rectangle Margin="2" SnapsToDevicePixels="true" Stroke="{DynamicResource {x:Static S

从0 开始 WPF MVVM 企业级框架实现与说明 ---- 第五讲 WPF中 Style

Style这个东西几乎是无处不在,这个类似于web开发中的css样式,想要做一个很丰富的UI,这个东西是必不可少的,我也不是专业的UI开发者,这边只能介绍Style在WPF中的用法 下面有一个下载地址,这个demo还可以供初学者学习 Style基本用法: 在WPF中我们可以使用Style来设置控件的某些属性值,并使该设置影响到指定范围内的所有该类控件或影响指定的某一控件,比如说我们想将窗口中的所有按钮都保持某一种风格,那么我们可以设置一个Style,而不必分别设置每个按钮的风格. Style是作

[WPF] 为Style 里的button添加鼠标点击响应事件

一个TabControl, 用的是PagedTabControl style, 在style中有个button, button在style里已经写了click事件,但是现在还需要加上一段功能,就是在响应事件之前对界面作一下判断.该怎么办呢?先看代码: 1. 控件XAML部分代码(位于文件form_loadatorigin.xaml): <!-- Form Body --> <TabControl x:Name="formLoadUnload" Style="

wpf 将Style应用到 ListView 中的 ListViewItem 元素

例: 为每个条目元素设置右键菜单 1. 新建右键菜单元素和样式元素 注意: 同时设置样式元素的 TargetType 属性和 x:Key 属性, 将样式元素限定为应用于 ListViewItem 类型元素并且需要显示指定才可应用 <Window.Resources> <ContextMenu x:Key="ContextMenuTest"> <MenuItem Header="右键菜单1"/> <MenuItem Heade

WPF系列 Style

    参考 WPF: Customize your Application with Styles and Control Templates (Part 2 of 2)

WPF CommonBox Style

<SolidColorBrush x:Key="ComboBoxItemBackground" Color="#FF868A8D" /> <Geometry x:Key="DownArrowGeometry">M 0 0 L 3.5 4 L 7 0 Z</Geometry> <SolidColorBrush x:Key="ListBorder" Color="#FF7F9DB