(22)WPF 控件模板
一、逻辑树和可视化树
二、模板
1.创建模板
<Window.Resources> <ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}"> <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red" TextBlock.Foreground="White" > <ContentPresenter RecognizesAccessKey="True" /> </Border> </ControlTemplate> </Window.Resources> <StackPanel> <Button x:Name="btn1" Template="{StaticResource ButtonTemplate}" Margin="10" Padding="5" >new2</Button> </StackPanel>
2.模板绑定
<ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"/>
3.添加触发器
<ControlTemplate x:Key="ButtonTemplate" TargetType="{x:Type Button}"> <Border Name="Border" BorderBrush="Orange" BorderThickness="3" CornerRadius="2" Background="Red" TextBlock.Foreground="White" > <ContentPresenter RecognizesAccessKey="True" Margin="{TemplateBinding Padding}"/> </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> </ControlTemplate.Triggers> </ControlTemplate>
焦点
xaml补
4.动画触发器
补
三、组织模板资源