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>
相关推荐
我是陈泽10 分钟前
一行 Python 代码能实现什么丧心病狂的功能?圣诞树源代码
开发语言·python·程序员·编程·python教程·python学习·python教学
优雅的小武先生21 分钟前
QT中的按钮控件和comboBox控件和spinBox控件无法点击的bug
开发语言·qt·bug
明耀21 分钟前
WPF RadioButton 绑定boolean值
c#·wpf
虽千万人 吾往矣27 分钟前
golang gorm
开发语言·数据库·后端·tcp/ip·golang
创作小达人30 分钟前
家政服务|基于springBoot的家政服务平台设计与实现(附项目源码+论文+数据库)
开发语言·python
郭二哈32 分钟前
C++——list
开发语言·c++·list
杨荧33 分钟前
【JAVA开源】基于Vue和SpringBoot的洗衣店订单管理系统
java·开发语言·vue.js·spring boot·spring cloud·开源
ZPC821040 分钟前
Python使用matplotlib绘制图形大全(曲线图、条形图、饼图等)
开发语言·python·matplotlib
镜花照无眠42 分钟前
Python爬虫使用实例-mdrama
开发语言·爬虫·python
aaasssdddd961 小时前
python和c
c语言·开发语言·python