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>
相关推荐
倔强青铜33 小时前
苦练Python第18天:Python异常处理锦囊
开发语言·python
u_topian4 小时前
【个人笔记】Qt使用的一些易错问题
开发语言·笔记·qt
珊瑚里的鱼4 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上4 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
xingshanchang4 小时前
Matlab的命令行窗口内容的记录-利用diary记录日志/保存命令窗口输出
开发语言·matlab
Risehuxyc4 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
时光追逐者4 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 46 期(2025年7.7-7.13)
c#·.net·.netcore
AI视觉网奇4 小时前
git 访问 github
运维·开发语言·docker
不知道叫什么呀5 小时前
【C】vector和array的区别
java·c语言·开发语言·aigc
mit6.8245 小时前
Why C# and .NET are still relevant in 2025
c#