原文:http://my.oschina.net/SeanZen/blog/82410
CheckBox 在默认的情况下是由一个正方形的框加后面的文字组成的。
在特殊的情况下,有可能需要我们用图片替换掉前面的正方形框体
不能直接给CheckBox设置背景来实现,参见代码:
1 | < CheckBox Content = "你好色彩" Margin = "16,289,275,0" VerticalAlignment = "Top" IsChecked = "True" > |
3 | < ImageBrush ImageSource = "images/check_normal.png" ></ ImageBrush > |
选中前:
选中后:
注意中间的“对勾”,严重影响视觉效果!先来看重写CheckBox之后的效果图:
来看代码吧。
样式
001 | < phone:PhoneApplicationPage.Resources > |
002 | < Style x:Key = "PhoneButtonBase" TargetType = "ButtonBase" > |
003 | < Setter Property = "Background" Value = "Transparent" /> |
004 | < Setter Property = "BorderBrush" Value = "{StaticResource PhoneForegroundBrush}" /> |
005 | < Setter Property = "Foreground" Value = "{StaticResource PhoneForegroundBrush}" /> |
006 | < Setter Property = "BorderThickness" Value = "{StaticResource PhoneBorderThickness}" /> |
007 | < Setter Property = "FontFamily" Value = "{StaticResource PhoneFontFamilySemiBold}" /> |
008 | < Setter Property = "FontSize" Value = "{StaticResource PhoneFontSizeMediumLarge}" /> |
009 | < Setter Property = "Padding" Value = "10,3,10,5" /> |
010 | < Setter Property = "Template" > |
012 | < ControlTemplate TargetType = "ButtonBase" > |
013 | < Grid Background = "Transparent" > |
014 | < VisualStateManager.VisualStateGroups > |
015 | < VisualStateGroup x:Name = "CommonStates" > |
016 | < VisualState x:Name = "Normal" /> |
017 | < VisualState x:Name = "MouseOver" /> |
018 | < VisualState x:Name = "Pressed" > |
020 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Foreground" Storyboard.TargetName = "ContentContainer" > |
021 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneBackgroundBrush}" /> |
022 | </ ObjectAnimationUsingKeyFrames > |
023 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Background" Storyboard.TargetName = "ButtonBackground" > |
024 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneForegroundBrush}" /> |
025 | </ ObjectAnimationUsingKeyFrames > |
026 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "BorderBrush" Storyboard.TargetName = "ButtonBackground" > |
027 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneForegroundBrush}" /> |
028 | </ ObjectAnimationUsingKeyFrames > |
031 | < VisualState x:Name = "Disabled" > |
033 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Foreground" Storyboard.TargetName = "ContentContainer" > |
034 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneDisabledBrush}" /> |
035 | </ ObjectAnimationUsingKeyFrames > |
036 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "BorderBrush" Storyboard.TargetName = "ButtonBackground" > |
037 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneDisabledBrush}" /> |
038 | </ ObjectAnimationUsingKeyFrames > |
039 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Background" Storyboard.TargetName = "ButtonBackground" > |
040 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "Transparent" /> |
041 | </ ObjectAnimationUsingKeyFrames > |
045 | </ VisualStateManager.VisualStateGroups > |
046 | < Border x:Name = "ButtonBackground" BorderBrush = "{TemplateBinding BorderBrush}" BorderThickness = "{TemplateBinding BorderThickness}" Background = "{TemplateBinding Background}" CornerRadius = "0" Margin = "{StaticResource PhoneTouchTargetOverhang}" > |
047 | < ContentControl x:Name = "ContentContainer" ContentTemplate = "{TemplateBinding ContentTemplate}" Content = "{TemplateBinding Content}" Foreground = "{TemplateBinding Foreground}" HorizontalContentAlignment = "{TemplateBinding HorizontalContentAlignment}" Padding = "{TemplateBinding Padding}" VerticalContentAlignment = "{TemplateBinding VerticalContentAlignment}" /> |
054 | < Style x:Key = "PhoneRadioButtonCheckBoxBase" BasedOn = "{StaticResource PhoneButtonBase}" TargetType = "ToggleButton" > |
055 | < Setter Property = "Background" Value = "{StaticResource PhoneRadioCheckBoxBrush}" /> |
056 | < Setter Property = "BorderBrush" Value = "{StaticResource PhoneRadioCheckBoxBrush}" /> |
057 | < Setter Property = "FontSize" Value = "{StaticResource PhoneFontSizeMedium}" /> |
058 | < Setter Property = "FontFamily" Value = "{StaticResource PhoneFontFamilyNormal}" /> |
059 | < Setter Property = "HorizontalContentAlignment" Value = "Left" /> |
060 | < Setter Property = "VerticalContentAlignment" Value = "Center" /> |
061 | < Setter Property = "Padding" Value = "0" /> |
063 | < Style x:Key = "CheckBoxStyle1" BasedOn = "{StaticResource PhoneRadioButtonCheckBoxBase}" TargetType = "CheckBox" > |
064 | < Setter Property = "Template" > |
066 | < ControlTemplate TargetType = "CheckBox" > |
067 | < Grid Background = "Transparent" > |
068 | < VisualStateManager.VisualStateGroups > |
069 | < VisualStateGroup x:Name = "CommonStates" > |
070 | < VisualState x:Name = "Normal" /> |
071 | < VisualState x:Name = "MouseOver" /> |
072 | < VisualState x:Name = "Disabled" > |
074 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Background" Storyboard.TargetName = "CheckBackground" > |
075 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneRadioCheckBoxDisabledBrush}" /> |
076 | </ ObjectAnimationUsingKeyFrames > |
077 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "BorderBrush" Storyboard.TargetName = "CheckBackground" > |
078 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneDisabledBrush}" /> |
079 | </ ObjectAnimationUsingKeyFrames > |
080 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Fill" Storyboard.TargetName = "CheckMark" > |
081 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneRadioCheckBoxCheckDisabledBrush}" /> |
082 | </ ObjectAnimationUsingKeyFrames > |
083 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Fill" Storyboard.TargetName = "IndeterminateMark" > |
084 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneRadioCheckBoxCheckDisabledBrush}" /> |
085 | </ ObjectAnimationUsingKeyFrames > |
086 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Foreground" Storyboard.TargetName = "ContentContainer" > |
087 | < DiscreteObjectKeyFrame KeyTime = "0" Value = "{StaticResource PhoneDisabledBrush}" /> |
088 | </ ObjectAnimationUsingKeyFrames > |
092 | < VisualStateGroup x:Name = "CheckStates" > |
093 | < VisualState x:Name = "Checked" > |
095 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Visibility" Storyboard.TargetName = "CheckMark" > |
096 | < DiscreteObjectKeyFrame KeyTime = "0" > |
097 | < DiscreteObjectKeyFrame.Value > |
098 | < Visibility >Visible</ Visibility > |
099 | </ DiscreteObjectKeyFrame.Value > |
100 | </ DiscreteObjectKeyFrame > |
101 | </ ObjectAnimationUsingKeyFrames > |
104 | < VisualState x:Name = "Unchecked" /> |
105 | < VisualState x:Name = "Indeterminate" > |
107 | < ObjectAnimationUsingKeyFrames Storyboard.TargetProperty = "Visibility" Storyboard.TargetName = "IndeterminateMark" > |
108 | < DiscreteObjectKeyFrame KeyTime = "0" > |
109 | < DiscreteObjectKeyFrame.Value > |
110 | < Visibility >Visible</ Visibility > |
111 | </ DiscreteObjectKeyFrame.Value > |
112 | </ DiscreteObjectKeyFrame > |
113 | </ ObjectAnimationUsingKeyFrames > |
117 | </ VisualStateManager.VisualStateGroups > |
118 | < Grid Margin = "{StaticResource PhoneTouchTargetLargeOverhang}" > |
119 | < Grid.ColumnDefinitions > |
120 | < ColumnDefinition Width = "40" /> |
121 | < ColumnDefinition Width = "*" /> |
122 | </ Grid.ColumnDefinitions > |
124 | < Border x:Name = "CheckBackground" BorderBrush = "{TemplateBinding Background}" BorderThickness = "0" HorizontalAlignment = "Left" Height = "40" IsHitTestVisible = "False" VerticalAlignment = "Center" Width = "40" > |
126 | < ImageBrush ImageSource = "images/check_normal.png" ></ ImageBrush > |
129 | < Rectangle x:Name = "IndeterminateMark" Fill = "{StaticResource PhoneRadioCheckBoxCheckBrush}" HorizontalAlignment = "Center" Height = "0" IsHitTestVisible = "False" Grid.Row = "0" Visibility = "Collapsed" VerticalAlignment = "Center" Width = "0" /> |
130 | < Canvas x:Name = "CheckMark" HorizontalAlignment = "Center" Height = "40" IsHitTestVisible = "False" Visibility = "Collapsed" VerticalAlignment = "Center" Width = "40" > |
132 | < ImageBrush ImageSource = "images/check_checked.png" ></ ImageBrush > |
135 | < ContentControl x:Name = "ContentContainer" ContentTemplate = "{TemplateBinding ContentTemplate}" Content = "{TemplateBinding Content}" Grid.Column = "1" Foreground = "{TemplateBinding Foreground}" HorizontalContentAlignment = "{TemplateBinding HorizontalContentAlignment}" Margin = "12,0,0,0" Padding = "{TemplateBinding Padding}" VerticalContentAlignment = "{TemplateBinding VerticalContentAlignment}" /> |
142 | </ phone:PhoneApplicationPage.Resources > |
调用
1 | < CheckBox Content = "你好色彩" Margin = "16,89,275,0" VerticalAlignment = "Top" IsChecked = "True" Style = "{StaticResource CheckBoxStyle1}" ></ CheckBox > |