WPF-控件模版 Template

空间模板

Template 模板

1 如何定义一个模板

每一个按钮空间里面都会有一个 template 标签,代表模版属性,一些这个控件的固有属性设置就在 template 标签里设置了。

xml 复制代码
  <Button>
      <Button.Template>
          <ControlTemplate>
              <Border  Background="Red" Height="40" Width="40" CornerRadius="5">
                  <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">中国</TextBlock>
              </Border>
          </ControlTemplate>
      </Button.Template>
  </Button>
  1. 如何复用一个模版
    将资源 template 定义在资源之上。对资源进行引用
xml 复制代码
<Window.Resources>
    <ControlTemplate x:Key="ButtonTemp">
        <Border  Background="Red" Height="40" Width="40" CornerRadius="5">
            <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">中国</TextBlock>
        </Border>
    </ControlTemplate>
</Window.Resources>
<Grid>
    <Button Template="{StaticResource ButtonTemp}">
    </Button>
</Grid>
  1. 如何绑定一个 template 的控件属性,自由定义属性,使用 Background="{TemplateBinding Background}" 定义绑定,再由使用时进行传输
xml 复制代码
 <Window.Resources>
     <ControlTemplate x:Key="ButtonTemp">
         <Border  Height="40" Width="40" CornerRadius="5"
                  Background="{TemplateBinding Background}">
             <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">中国</TextBlock>
         </Border>
     </ControlTemplate>
<Grid>
     <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center">
         <Button Template="{StaticResource ButtonTemp}" Background="Green">
         </Button>
         <Button Template="{StaticResource ButtonTemp}" Background="Orange">
         </Button>
     </StackPanel>
</Grid>
 </Window.Resources>
  1. 如果你想给 template 设置默认属性,可以添加额外的 setter 来实现, 本身 template 不支持
xml 复制代码
 <Window.Resources>
     <ControlTemplate x:Key="ButtonTemp" TargetType="Button">
         <Border Height="40" Width="40" CornerRadius="5"
             Background="{TemplateBinding Background}">
             <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center">中国</TextBlock>
         </Border>
     </ControlTemplate>
     <Style TargetType="Button">
         <Setter Property="Template" Value="{StaticResource ButtonTemp}" />
         <Setter Property="Background" Value="Red" />
     </Style>
 </Window.Resources>

 <Grid>
     <StackPanel HorizontalAlignment="Left" VerticalAlignment="Center">
         <Button Background="Green"></Button>
         <Button Background="Orange"></Button>
         <Button></Button>
         <!-- 这个按钮将使用默认的红色背景 -->
     </StackPanel>
 </Grid>

Template 模板触发器

如何定义要给模板触发器

  1. 使用 ControlTemplate.Triggers 标签。

    xml 复制代码
    <!--定义一个触发器-->
    <Window.Resources>
        <ControlTemplate x:Key="ButtonTemp" TargetType="Button">
            <Border Name="border" Height="40" Width="40" CornerRadius="5"
                Background="{TemplateBinding Background}">
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="25"></ColumnDefinition>
                        <ColumnDefinition Width="25"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <ContentPresenter Grid.Column="1"  HorizontalAlignment="Center" VerticalAlignment="Center" />
                    <TextBlock Grid.Column="0" Text="A" HorizontalAlignment="Center" VerticalAlignment="Center" ></TextBlock>
                </Grid>
            </Border>
            <!--定义一个触发器-->
            <ControlTemplate.Triggers>
               <!-- TargetName 定义触发器的作用域的范围-->
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" TargetName="border"
                 Value="#FFBEE6FD"/>
                    <Setter Property="BorderBrush" TargetName="border"
                 Value="#FF3C7FB1"/>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
        <Style TargetType="Button">
            <Setter Property="Template" Value="{StaticResource ButtonTemp}" />
            <Setter Property="Background" Value="Red" />
            <Setter Property="Content" Value="AAA" />
        </Style>
    </Window.Resources>
相关推荐
暮雪倾风4 小时前
【WPF开发】超级详细的“文件选择”(附带示例工程)
windows·wpf
明耀8 小时前
WPF RadioButton 绑定boolean值
c#·wpf
暮雪倾风9 小时前
【WPF开发】控件介绍-Grid(网格布局)
windows·wpf
芝麻科技2 天前
使用ValueConverters扩展实现枚举控制页面的显示
wpf·prism
笑非不退2 天前
Wpf Image 展示方式 图片处理 显示
开发语言·javascript·wpf
△曉風殘月〆3 天前
在WPF中实现多语言切换的四种方式
wpf·多语言切换
笑非不退3 天前
WPF C# 读写嵌入的资源 JSON PNG JPG JPEG 图片等资源
c#·wpf
He BianGu3 天前
演示:基于WPF的DrawingVisual开发的频谱图和律动图
wpf·示波器·曲线图·频谱分析仪·频谱图·高性能曲线·自绘
笑非不退6 天前
WPF 设计属性 设计页面时实时显示 页面涉及集合时不显示处理 设计页面时显示集合样式 显示ItemSource TabControl等集合样式
wpf
△曉風殘月〆7 天前
WPF中的XAML详解
wpf·xaml