如何给CheckBox设定控件模板
来源:互联网 发布:淘宝教育客户端下载 编辑:程序博客网 时间:2024/06/11 23:00
参考资料:
1. http://technet.microsoft.com/zh-cn/library/hh465374
2. http://msdn.microsoft.com/zh-cn/library/windows/apps/hh465045.aspx
先看下面我根据参考资料1修改后的代码,便于理解。
<!-- HH Add a new ControlTemplate for checkbox --><ControlTemplate x:Key="CheckBoxTemplate1" TargetType="CheckBox" > <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"> <VisualStateManager.VisualStateGroups> <VisualStateGroup x:Name="CheckStates"> <VisualState x:Name="Checked"> <Storyboard> <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="CheckGlyph"/> </Storyboard> </VisualState> <VisualState x:Name="Unchecked"/> <VisualState x:Name="Indeterminate"> <Storyboard> <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="IndeterminateGlyph"/> </Storyboard> </VisualState> </VisualStateGroup> </VisualStateManager.VisualStateGroups> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="25"/> </Grid.RowDefinitions> <Rectangle x:Name="NormalRectangle" Fill="White" Stroke="Black" StrokeThickness="1" UseLayoutRounding="True" Height="21" Width="21"/> <!-- Create an X to indicate that the CheckBox is selected. --> <Path x:Name="CheckGlyph" Data="M103,240 L111,240 119,248 127,240 135,240 123,252 135,264 127,264 119,257 111,264 103,264 114,252 z" Fill="Black" FlowDirection="LeftToRight" Height="14" Width="16" Opacity="0" Stretch="Fill"/> <!-- Create an Ellipse to indicate that the CheckBox is in an indeterminate state. --> <Ellipse x:Name="IndeterminateGlyph" Fill="Black" Height="12" Width="12" Opacity="0"/> <ContentPresenter Grid.Row="1" x:Name="ContentPresenter" ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" Margin="{TemplateBinding Padding}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Grid> </Border> </ControlTemplate>
具体实现的界面图,可以看参考资料1,那里面很详细。
注意点:
1. 请看Path以及Ellipse的Opacity属性值为0,表示默认情况下这2个属性不显示。
2. Storyboard表示控件模板中元素的外观。当控件切换到 VisualState.Name 属性指定的状态时,Storyboard 就会开
始。当控件退出该状态时,Storyboard 就会停止。
3. 当控件状态(即VisualState)切换到Checked的时候,Storyboard.TargetProperty="Opacity以及
Storyboard.TargetName="CheckGlyph"表示设置下面Grid的元素CheckGlyph中的Opacity为1,这样就将
CheckGlyph显示出来了。其他原理类似。
4.
Stroke :说明如何绘制形状的轮廓。
StrokeThickness :说明形状轮廓的粗细。
Fill :说明如何绘制形状的内部。
///////////////////
新手使用方法:
1. 新建一个Blank App(XAML),修改MainPage.xaml为:
<Grid Background="{StaticResource ApplicationPageBackgroundThemeBrush}"> <CheckBox Content="CheckBox" Template="{StaticResource CheckBoxTemplate1}" IsThreeState="True" Margin="68,361,1223,328" Height="79"/></Grid>
2. 再将上述
<ControlTemplate ...> ... </<ControlTemplate >
部分代码添加到App.xaml中的<ResourceDictionary></ResourceDictionary>之间。
- 如何给CheckBox设定控件模板
- 如何给checkbox创建setOnCheckedChangeListener()?
- 如何给 legend 标签设定宽度
- CheckBox控件如何自动Checked?
- (CEGUI)如何创建控件 ------Checkbox
- mfc evc CheckBox控件被Group Box控件给遮盖
- 给TreeView控件的CheckBox添加PostBack脚本程序
- 给 winform datagridview 头部 添加 checkbox 全选控件
- umbraco如何设定模板编辑特定类型的数据?
- 设定下拉列表控件DropDownList,复选按钮CheckBox,RadioButton默认值总结测试
- GridView控件中模板列CheckBox全选、反选、取消
- 关于如何设定winForm中某个控件的显示位置
- DataGrid Checkbox 数据设定、取值 如何通过post请求向后台提交[]数组
- CheckBox控件
- CheckBox控件
- checkbox控件
- CheckBox控件
- CheckBox控件
- hdu 1143
- 第八章 启动与执行业务流程(八)
- 华容道游戏VB.NET 2010 版28连局
- 第八章 启动与执行业务流程(九)
- 学历是铜牌,能力是银牌,人脉是金牌,思维是王牌
- 如何给CheckBox设定控件模板
- 堆和栈的区别 (转贴1)
- ajax提交form表单
- 新股停牌规则
- 深入浅出C/C++中的正则表达式库(二)——Boost.Regex
- jquery与ajax的应用
- Android : 把一个layout转化成一个Drawable的实例
- 打开新窗口
- Android 闹钟源码