Windows Phone 7 控件重写之CheckBox 图片切换

原文:http://my.oschina.net/SeanZen/blog/82410

CheckBox 在默认的情况下是由一个正方形的框加后面的文字组成的。

在特殊的情况下,有可能需要我们用图片替换掉前面的正方形框体

不能直接给CheckBox设置背景来实现,参见代码:

1 <CheckBox Content="你好色彩"  Margin="16,289,275,0" VerticalAlignment="Top" IsChecked="True">
2                 <CheckBox.Background>
3                     <ImageBrush ImageSource="images/check_normal.png"></ImageBrush>
4                 </CheckBox.Background>
5 </CheckBox>

选中前:

选中后:

 

注意中间的“对勾”,严重影响视觉效果!先来看重写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">
011                 <Setter.Value>
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">
019                                         <Storyboard>
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>
029                                         </Storyboard>
030                                     </VisualState>
031                                     <VisualState x:Name="Disabled">
032                                         <Storyboard>
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>
042                                         </Storyboard>
043                                     </VisualState>
044                                 </VisualStateGroup>
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}"/>
048                             </Border>
049                         </Grid>
050                     </ControlTemplate>
051                 </Setter.Value>
052             </Setter>
053         </Style>
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"/>
062         </Style>
063         <Style x:Key="CheckBoxStyle1" BasedOn="{StaticResource PhoneRadioButtonCheckBoxBase}" TargetType="CheckBox">
064             <Setter Property="Template">
065                 <Setter.Value>
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">
073                                         <Storyboard>
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>
089                                         </Storyboard>
090                                     </VisualState>
091                                 </VisualStateGroup>
092                                 <VisualStateGroup x:Name="CheckStates">
093                                     <VisualState x:Name="Checked">
094                                         <Storyboard>
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>
102                                         </Storyboard>
103                                     </VisualState>
104                                     <VisualState x:Name="Unchecked"/>
105                                     <VisualState x:Name="Indeterminate">
106                                         <Storyboard>
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>
114                                         </Storyboard>
115                                     </VisualState>
116                                 </VisualStateGroup>
117                             </VisualStateManager.VisualStateGroups>
118                             <Grid Margin="{StaticResource PhoneTouchTargetLargeOverhang}">
119                                 <Grid.ColumnDefinitions>
120                                     <ColumnDefinition Width="40"/>
121                                     <ColumnDefinition Width="*"/>
122                                 </Grid.ColumnDefinitions>
123  
124                                 <Border x:Name="CheckBackground" BorderBrush="{TemplateBinding Background}" BorderThickness="0" HorizontalAlignment="Left" Height="40" IsHitTestVisible="False" VerticalAlignment="Center" Width="40">
125                                     <Border.Background>
126                                         <ImageBrush ImageSource="images/check_normal.png"></ImageBrush>
127                                     </Border.Background>
128                                 </Border>
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">
131                                     <Canvas.Background>
132                                         <ImageBrush ImageSource="images/check_checked.png"></ImageBrush>
133                                     </Canvas.Background>
134                                 </Canvas>
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}"/>
136                             </Grid>
137                         </Grid>
138                     </ControlTemplate>
139                 </Setter.Value>
140             </Setter>
141         </Style>
142     </phone:PhoneApplicationPage.Resources>

调用

1 <CheckBox Content="你好色彩" Margin="16,89,275,0" VerticalAlignment="Top" IsChecked="True" Style="{StaticResource CheckBoxStyle1}"></CheckBox>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值