WPF之自定义控件模版

WPF控件模版是在window标签下使用Resources属性定义的模版,在controltemplate中设置键然后再通过键进行调用的一个过程。 通常用于界面中重复性较多的自定义控件时使用。

代码如下 :
第一步:(封装模版)
cs 复制代码
// 封装模版
<Window.Resources>
    <!--封装自定义模版-->
<!--ControlTemplate 定义模版数据源
key 设置模版的键,以后通过键找到对应模版
<!--TargetType = "GroupBox"模版使用在哪种标签下-->
    <ControlTemplate x:Key="c1"
                     TargetType="Button">
        <Border Background="AliceBlue"
                CornerRadius="5"
                BorderThickness="2"
                x:Name="border">
            <!--ContentPresenter 呈现内容的标签-->
            <StackPanel Orientation="Horizontal">
                <TextBlock VerticalAlignment="Center">》》</TextBlock>
                <ContentPresenter HorizontalAlignment="Center"
                                  VerticalAlignment="Center">
                </ContentPresenter>
            </StackPanel>
        </Border>
        <!--Triggers 设置触发 鼠标移去 鼠标移开效果-->
        <ControlTemplate.Triggers>
            <!--Property设置属性
            Value属性值-->
            <!--IsMouseOver 鼠标放上去-->
            <Trigger Property="IsMouseOver"
                     Value="true">
                <Setter Property="Background"
                        Value="red"
                        TargetName="border">
                </Setter>
                <Setter Property="BorderBrush"
                        Value="green"
                        TargetName="border">
                    
                </Setter>
            </Trigger>
        </ControlTemplate.Triggers>
    </C
第二步通过键进行调用 :
cs 复制代码
// 调用自定义模版
<Button Template="{StaticResource c1}"
        Width="100"
        Height="40"
        Content="删除">
</Buton>
相关推荐
xuzhiqiang07243 小时前
Java进阶之路,Java程序员职业发展规划
java·开发语言
MediaTea3 小时前
Python:生成器表达式详解
开发语言·python
overmind5 小时前
oeasy Python 115 列表弹栈用pop删除指定索引
开发语言·python
Never_Satisfied6 小时前
在c#中,使用windows自带功能将文件夹打包为ZIP
开发语言·windows·c#
hnxaoli6 小时前
win10程序(十六)通达信参数清洗器
开发语言·python·小程序·股票·炒股
电饭叔6 小时前
文本为 “ok”、前景色为白色、背景色为红色,且点击后触发 processOK 回调函数的 tkinter 按钮
开发语言·python
Never_Satisfied7 小时前
在c#中,string.replace会替换所有满足条件的子字符串,如何只替换一次
开发语言·c#
听麟8 小时前
HarmonyOS 6.0+ 跨端会议助手APP开发实战:多设备接续与智能纪要全流程落地
分布式·深度学习·华为·区块链·wpf·harmonyos
Demon_Hao9 小时前
JAVA快速对接三方支付通道标准模版
java·开发语言