其他分享
首页 > 其他分享> > WPF控件模板

WPF控件模板

作者:互联网

控件模板:

控件模板可以对控件外观如何修饰,定义用ControlTemplate

如按钮的控件模板定义

<ControlTemplate TargetType="{x:Type Button}">
<Border x:Name="border" BorderBrush="#FF24C6F5"
BorderThickness="{TemplateBinding BorderThickness}"
SnapsToDevicePixels="true"
CornerRadius="5">
<Border.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF24C6F5" Offset="0.606"/>
<GradientStop Color="White" Offset="0.294"/>
</LinearGradientBrush>
</Border.Background>
<Grid>
<Border Name="light" Visibility="Collapsed">
<Border.Background>
<RadialGradientBrush RadiusY="1" Center="0.5,1" GradientOrigin="0.5,1">
<GradientStop Color="White" Offset="0.424"/>
<GradientStop Offset="1"/>
</RadialGradientBrush>
</Border.Background>
</Border>
<ContentPresenter x:Name="contentPresenter" Focusable="False" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> //ContentPresenter占位按钮的内容显示

</Grid>
</Border>
<ControlTemplate.Triggers>

//控件模板里面可以定义触发器
<Trigger Property="IsDefaulted" Value="true">
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Visibility" Value="Visible" TargetName="light"/>
<!--<Setter Property="Background" TargetName="border" Value="{StaticResource Button.MouseOver.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.MouseOver.Border}"/>-->
</Trigger>
<Trigger Property="IsPressed" Value="true">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Pressed.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Pressed.Border}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="border" Value="{StaticResource Button.Disabled.Background}"/>
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource Button.Disabled.Border}"/>
<Setter Property="TextElement.Foreground" TargetName="contentPresenter" Value="{StaticResource Button.Disabled.Foreground}"/>
</Trigger>

</ControlTemplate.Triggers>
</ControlTemplate>

TemplateBinding:可以引用控件里面的属性设置等价于  TemplateBinding={Binding RelativeSource={RelativeSource TemplateParent}}

 

标签:控件,TemplateBinding,定义,RelativeSource,BorderThickness,WPF,模板
来源: https://www.cnblogs.com/zyouliang/p/15573173.html