码迷,mamicode.com
首页 > 其他好文 > 详细

控件模板学习笔记(二)

时间:2015-01-19 19:05:41      阅读:201      评论:0      收藏:0      [点我收藏+]

标签:

1.模板绑定TemplateBinding

什么情况下使用模板绑定?

--当您仍可能想要使用公共属性更改控件的外观时,模板绑定是您的不二之选

怎么使用模板绑定?

--我们还以学习笔记一中的Button为例,自定义模板中的Border的Background=“Red”,使用TemplateBinding则为Background=“{TemplateBinding Backbround}”;

等号左边的Background为Border的背景颜色,等号右边的Background为Button的属性。

使用模板绑定能够解决什么样的问题?

--当多个按钮呈现的可视化结构相同,只是背景颜色或是边框颜色不一样时,如果不使用TemplateBinding,可能您就要为每一个按钮定义一个控件模板,这些控件模板相差的

也许只是一个属性。所以为了解决这种情况我们可以使用TemplateBinding,示例如下:

技术分享
<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
            <Border Name="Border" BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="{TemplateBinding Background}" TextBlock.Foreground="White" SnapsToDevicePixels="True">
                <Grid>
                    <Rectangle Name="FocusCue" Visibility="Hidden" Stroke="Black"
                               StrokeThickness="1" StrokeDashArray="1 2" SnapsToDevicePixels="True"></Rectangle>
                    <ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"></ContentPresenter>
                </Grid>
            </Border>
            <ControlTemplate.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="DarkRed" />
                </Trigger>
                <Trigger Property="IsPressed" Value="true">
                    <Setter TargetName="Border" Property="Background" Value="IndianRed" />
                    <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" />
                </Trigger>
                <Trigger Property="IsKeyboardFocused" Value="True">
                    <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" />
                </Trigger>
            </ControlTemplate.Triggers>
</ControlTemplate>
控件模板
<Button Content="ButtonA" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" Background="Red" Template="{StaticResource ButtonTemplate}" />
<Button Content="ButtonB" HorizontalAlignment="Left" Margin="10,47,0,0" VerticalAlignment="Top" Width="75" Background="Green" Template="{StaticResource ButtonTemplate}" />

效果如图:技术分享

2.自动应用模板

自动应用模板是不设置Template=“{StaticResource ButtonTemplate}”时,项目中的按钮自动应用定义好的控件模板。

怎么实现自动应用模板?
--技巧是使用类型样式,这种样式会自动影响相应的元素类型并设置Template属性,示例如下:

<Style TargetType="{x:Type Button}">
            <Setter Property="Control.Template" Value="{StaticResource ButtonTemplate}" />
</Style>

注:虽然在Resources里定义,但是没有定义x:Key属性。其中的ButtonTemplate为学习笔记一中定义的控件模板

<Button Content="ButtonA" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Width="75" />
<Button Content="ButtonB" HorizontalAlignment="Left" Margin="10,75,0,0" VerticalAlignment="Top" Width="75"  />

效果如图:技术分享,其实从工具箱里新添加一个按钮也会自动应用该控件模板,只是现在我还不会制作和上传Gif类型的文件,没办法贴效果图。

 

控件模板学习笔记(二)

标签:

原文地址:http://www.cnblogs.com/Seesaw132/p/4234423.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!