首页 > 代码库 > WPF编程宝典之控件模版(七)

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

将控件模版定义为资源,并使用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="http://www.mamicode.com/Gray" />
3     </Setter TargetName="Border" Property="TextBlock.Background" Value="http://www.mamicode.com/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编程宝典之控件模版(七)