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>
相关推荐
哲Zheᗜe༘21 小时前
了解学习Python编程之python基础
开发语言·python·学习
落日漫游21 小时前
数据结构笔试核心考点
java·开发语言·算法
寻找华年的锦瑟21 小时前
Qt-配置文件(INI/JSON/XML)
开发语言·qt
HY小海1 天前
【C++】AVL树实现
开发语言·数据结构·c++
workflower1 天前
Fundamentals of Architectural Styles and patterns
开发语言·算法·django·bug·结对编程
Roc-xb1 天前
ModuleNotFoundError: No module named ‘conda_token‘
开发语言·python·conda
人工干智能1 天前
Python 开发中:`.ipynb`(Jupyter Notebook 文件)和 `.py`(Python 脚本文件)
开发语言·python·jupyter
Jay-juice1 天前
QT信号与槽
开发语言·qt
fs哆哆1 天前
在VB.NET中,有没有 ?.这个运算符
java·开发语言·.net
驰羽1 天前
[GO]什么是热重载,如何使用Air工具
开发语言·后端·golang