wpf之控件模板

 下面我们创建一个button控件

复制代码
<UserControl x:Class="MyWpf.ControlTemplateTest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyWpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources> 
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}"> 
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Border  x:Name="border" CornerRadius="15" BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ContentPresenter Content="测试" x:Name="contentPresenter" Focusable="False"
HorizontalAlignment
="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}" RecognizesAccessKey="True"

SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </Setter.Value> </Setter> </Style> </UserControl.Resources> <Grid> <Button Width="100" Height="50" Style="{DynamicResource ButtonStyle1}"></Button> </Grid> </UserControl>
复制代码

 

 界面:

 

 可以在Border标签中设置button的样式,当然也可以不在style标签中写也可以按照下面方式写:

复制代码
<UserControl x:Class="MyWpf.ControlTemplateTest"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:MyWpf"
             mc:Ignorable="d" 
             d:DesignHeight="450" d:DesignWidth="800">
    <UserControl.Resources> 
        <ControlTemplate x:Key="buttonTemplate" TargetType="Button">
                <Border  x:Name="border" CornerRadius="15" BorderBrush="{TemplateBinding BorderBrush}" 
BorderThickness
="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"> <ContentPresenter Content="测试" x:Name="contentPresenter" Focusable="False"
HorizontalAlignment
="{TemplateBinding HorizontalContentAlignment}" Margin="{TemplateBinding Padding}"
RecognizesAccessKey
="True" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
VerticalAlignment
="{TemplateBinding VerticalContentAlignment}"/> </Border> </ControlTemplate> </UserControl.Resources> <Grid> <Button Width="100" Template="{StaticResource buttonTemplate}" Height="50" ></Button> </Grid> </UserControl>
复制代码

 

 

其它优秀博客:https://www.cnblogs.com/dingli/archive/2011/07/20/2112150.html,https://www.cnblogs.com/minhost/p/7600551.html

 

posted @   安静点--  阅读(120)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示