继续聊WPF——自定义CheckBox控件外观

  
上一篇文章中谈到了BulletDecorator控件,就是为自定义CheckBox控件的模板做准备,因为CheckBox需要比较严格的布局,正好,BulletDecorator控件就合适了,该控件的布局是有项目列表,排列起来好办很多了。
 
第一步,先建立一项资源,就是控件的聚焦样式,即当你在窗体中不断按Tab键使控件获取焦点时的样式,后面要用到。
  1. <!--当控件获得键盘焦点时的样式-->  
  2. <Style x:Key="FocusStyle">  
  3.     <Setter Property="Control.Template">  
  4.         <Setter.Value>  
  5.             <ControlTemplate>  
  6.                 <Rectangle Stroke="Red" StrokeThickness="1"/>  
  7.             </ControlTemplate>  
  8.         </Setter.Value>  
  9.     </Setter>  
  10. </Style>  

第二步,写好CheckBox的样式。

  1. <!-- 
  2.     CheckBox的样式 
  3. -->  
  4. <Style TargetType="{x:Type CheckBox}">  
  5.     <Setter Property="OverridesDefaultStyle" Value="True"/>  
  6.     <Setter Property="FocusVisualStyle" Value="{StaticResource FocusStyle}"/>  
  7.     <Setter Property="SnapsToDevicePixels" Value="True"/>  
  8.     <Setter Property="Foreground" Value="White"/>  
  9.     <Setter Property="FontSize" Value="16"/>  
  10.     <Setter Property="Template">  
  11.         <Setter.Value>  
  12.             <ControlTemplate TargetType="{x:Type CheckBox}">  
  13.                 <BulletDecorator FlowDirection="LeftToRight" VerticalAlignment="Center">  
  14.                     <BulletDecorator.Bullet>  
  15.                         <Border x:Name="bd"  
  16.                                 BorderThickness="1"  
  17.                                 BorderBrush="Red"  
  18.                                 MinHeight="15"  
  19.                                 MinWidth="15"  
  20.                                 VerticalAlignment="Center">  
  21.                             <Border.Background>  
  22.                                 <LinearGradientBrush StartPoint="0,0"  
  23.                                                      EndPoint="1,1">  
  24.                                     <GradientStop Color="LightGray" Offset="0.2"/>  
  25.                                     <GradientStop Color="White" Offset="1"/>  
  26.                                 </LinearGradientBrush>  
  27.                             </Border.Background>  
  28.                             <Path x:Name="cp" Width="12" Height="12"  
  29.                                   Stroke="Blue"  
  30.                                   StrokeThickness="3"/>  
  31.                         </Border>  
  32.                     </BulletDecorator.Bullet>  
  33.                     <ContentPresenter Margin="2,0"/>  
  34.                 </BulletDecorator>  
  35.                 <!-- 
  36.                     控件触发器 
  37.                 -->  
  38.                 <ControlTemplate.Triggers>  
  39.                     <Trigger Property="IsChecked" Value="True">  
  40.                         <!-- 画上一个勾 -->  
  41.                         <Setter TargetName="cp" Property="Data"  
  42.                                 Value="M 0,6 L 6,12 12,0"/>  
  43.                         <Setter Property="Foreground" Value="LightGreen"/>  
  44.                     </Trigger>  
  45.                     <Trigger Property="IsMouseOver" Value="True">  
  46.                         <Setter TargetName="bd" Property="Background">  
  47.                             <Setter.Value>  
  48.                                 <LinearGradientBrush StartPoint="0,0" EndPoint="1,1">  
  49.                                     <GradientStop Color="Orange" Offset="0.12"/>  
  50.                                     <GradientStop Color="Yellow" Offset="0.92"/>  
  51.                                 </LinearGradientBrush>  
  52.                             </Setter.Value>  
  53.                         </Setter>  
  54.                     </Trigger>  
  55.                 </ControlTemplate.Triggers>  
  56.             </ControlTemplate>  
  57.         </Setter.Value>  
  58.     </Setter>  
  59. </Style>  

 

第三步,把窗体默认的Grid控件去掉,为了更好地布局,应使用StatcPanel控件。
  1. <StackPanel Orientation="Vertical" Margin="20,20,20,20">  
  2.     <CheckBox Content="苹果"/>  
  3.     <CheckBox Content="鸡蛋"/>  
  4.     <CheckBox Content="白菜"/>  
  5.     <CheckBox Content="萝卜"/>  
  6.     <CheckBox Content="豆浆"/>  
  7.     <CheckBox Content="咸菜"/>  
  8.     <CheckBox Content="炒饭"/>  
  9.     <CheckBox Content="烧鸭饭"/>  
  10.     <CheckBox Content="叉烧饭"/>   
  11. </StackPanel>  


 

好,完事,现在来看看效果吧。

 当项目被选中后,字体自动变为绿色,请参照上面的XAML代码。

怎么样,漂亮不?


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值