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>
相关推荐
攻城狮CSU2 小时前
WPF中核心接口 INotifyPropertyChanged
wpf
c#上位机2 小时前
wpf之Interaction.Triggers
c#·wpf
是木子啦6 小时前
wpf passwordbox控件 光标移到最后
c#·wpf
The Sheep 20236 小时前
wpf 命令理解
wpf
布伦鸽6 小时前
C# WPF DataGrid使用Observable<Observable<object>类型作为数据源
开发语言·c#·wpf
分布式存储与RustFS18 小时前
告别复杂配置:用Milvus、RustFS和Vibe Coding,60分钟DIY专属Chatbot
wpf·文件系统·milvus·对象存储·minio·rustfs·vibe
攻城狮CSU1 天前
WPF 绑定机制实现原理
wpf
攻城狮CSU1 天前
WPF 之数据绑定一(Data Binding)
wpf
wuty0072 天前
记录一下 WPF进程 SendMessage 发送窗口消息进行进程间通信,存在进程权限无法接受消息的问题
wpf·进程间通信·sendmessage·进程权限
c#上位机2 天前
wpf之ToggleButton控件
c#·wpf