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>
相关推荐
小小小米粒1 天前
Collection单列集合、Map(Key - Value)双列集合,多继承实现。
java·开发语言·windows
czhc11400756631 天前
C# 428 线程、异步
开发语言·c#
:1211 天前
java基础
java·开发语言
唐青枫1 天前
C#.NET ThreadLocal 深入解析:线程独享数据、性能收益与实战边界
c#·.net
SilentSamsara1 天前
Python 环境搭建完整指南:从下载安装到运行第一个程序
开发语言·python
小短腿的代码世界1 天前
Qt文件系统与IO深度解析:从QFile到异步文件操作
开发语言·qt
harder3211 天前
RMP模式的创新突破
开发语言·学习·ios·swift·策略模式
jinanwuhuaguo1 天前
OpenClaw工程解剖——RAG、向量织构与“记忆宫殿”的索引拓扑学(第十三篇)
android·开发语言·人工智能·kotlin·拓扑学·openclaw
Rust研习社1 天前
使用 Axum 构建高性能异步 Web 服务
开发语言·前端·网络·后端·http·rust
淘矿人1 天前
从0到1:用Claude启动你的第一个项目
开发语言·人工智能·git·python·github·php·pygame