码迷,mamicode.com
首页 > Windows程序 > 详细

WPF编程宝典之控件模版(七)

时间:2016-12-19 21:21:06      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:block   route   log   led   dict   提取   smo   die   nes   

将控件模版定义为资源,并使用StaticResource引用该资源

1   <Button Margin="10" Padding="5" Template="{StaticResource ButtonTemplaate}">
2    A Simple Button with a Custom Template</Button>

控件模版的基本框架如下:

1 <Window.Resource>
2     <ControlTemplate x:Key="ButtonTemplate" Target="{x:Type Button}">
3     ...
4     </ControlTemplate>
5 </Window.Resource>

 

2.简单按钮

WPF通过使用模版绑定,可以从应用模版的控件中提取一个值。

1  <Window.Resource>
2     <ControlTemplate x:Key="ButtonTemplate" Target="{x:Type Button}">
3         <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2"
4          Background="Red" TextBlock.Foreground="White">
5          <ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"></ContentPresenter>
6     </ControlTemplate>
7 </Window.Resource>

模版绑定支持WPF的变化监测基础结构,所有依赖项属性都包含该基础结构。这意味着如果修改了控件的属性,模版会自动考虑该变化。当使用在一小段时间内重复改变属性值的动画时,该细节尤其有用。

 

为控件添加事件处理方法:

 1 <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}">
 2     <Border Name="Border" BorderBrush="Orange" .... >
 3     </Border>
 4     <ControlTemplate.Triggers>
 5         <Trigger Property="IsMouseOver" Value="True">
 6         <Setter TargetName="Border" Property="Background" Value="DarkRed" />
 7         </Trigger>
 8         /*
 9         * 可以添加更多的事件
10         */
11     </ControlTemplate.Triggers>
12 </ControlTemplate>

注意,对于禁用功能,一般放在最后,,在触发器列表的末尾定义,这样可以保证IsEnabled属性触发器具有最高的优先权

1 <Trigger Property="IsEnabled" Value="False">
2     </Setter TargetName="Border" Property="TextBlock.Foreground" Value="Gray" />
3     </Setter TargetName="Border" Property="TextBlock.Background" Value="MistyRose" />
4 </Trigger>

 

3.应用动画的触发器

 1 <ControlTemplate.Triggers>
 2     <EventTrigger RoutedEvent="MouseEnter">
 3         <BeginStoryboard>
 4             <Storyboard>
 5                 <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color"
 6                                 To="Blue" Duration="0:0:1" AutoReverse="True" RepeatBehavior="Forever"></ColorAnimation>
 7             </Storyboard>
 8         </BeginStoryboard>
 9     </EventTrigger>
10     <EventTrigger RoutedEvent="MouseLeave">
11         <BeginStoryboard>
12             <Storyboard>
13                 <ColorAnimation Storyboard.TargetName="Border" Storyboard.TargetProperty="Background.Color" Duration="0:0:0.5">
            </
ColorAnimation> 14 </Storyboard> 15 </BeginStoryboard> 16 </EventTrigger> 17 <Trigger Property="IsPressed" Value="True"> 18 <Setter TargetName="Border" Property="Background" Value="IndianRed" /> 19 <Setter TargetName="Border" Property="BorderBrush" Value="DarkKhaki" /> 20 </Trigger> 21 <Trigger Property="IsKeyboardFocused" Value="True"> 22 <Setter TargetName="FocusCue" Property="Visibility" Value="Visible" /> 23 </Trigger> 24 </ControlTemplate.Triggers>

 

4.通过代码修改资源

1     ResourceDictionary resourceDictionary = new ResourceDictionary();
2     resourceDictionary.Source = new Uri(
3         "Resources/GradientButtonVariant.xaml", UriKind.Relative);
4     this.Resources.MergedDictionaries[0] = resourceDictionary; 

 

WPF编程宝典之控件模版(七)

标签:block   route   log   led   dict   提取   smo   die   nes   

原文地址:http://www.cnblogs.com/imstrive/p/6198785.html

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