WPF自定义模板--Lable

目录

属性:

外观属性

内容属性

布局属性

字体属性

其他属性

触发器属性

完整案例:


属性:

外观属性
  • Background:背景色
  • BorderBrush:边框颜色
  • BorderThickness:边框厚度
  • CornerRadius:圆角半径(适用于Border控件)
内容属性
  • Content:内容
  • ContentTemplate:内容模板
  • ContentStringFormat:内容字符串格式
布局属性
  • Padding:内部填充
  • HorizontalContentAlignment:水平内容对齐方式
  • VerticalContentAlignment:垂直内容对齐方式
字体属性
  • FontFamily:字体格式
  • FontSize:字体大小
  • FontStretch:字体拉伸
  • FontStyle:字体样式
  • FontWeight:字体粗细
其他属性
  • Foreground:前景色(文本颜色)
  • Opacity:不透明度
  • Visibility:可见性
  • IsEnabled:是否启用
触发器属性
  • IsMouseOver:鼠标悬停
  • IsFocused:是否获得焦点
  • IsEnabled:是否启用

完整案例:

<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <Style x:Key="CustomLabelStyle" TargetType="Label">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="Label">
                        <Border x:Name="border" 
                                Background="{TemplateBinding Background}" 
                                BorderBrush="{TemplateBinding BorderBrush}" 
                                BorderThickness="{TemplateBinding BorderThickness}"
                                CornerRadius="5"
                                Padding="{TemplateBinding Padding}"
                                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                VerticalAlignment="{TemplateBinding VerticalContentAlignment}">
                            <ContentPresenter HorizontalAlignment="Center" 
                                              VerticalAlignment="Center" 
                                              Content="{TemplateBinding Content}"
                                              ContentTemplate="{TemplateBinding ContentTemplate}"
                                              ContentStringFormat="{TemplateBinding ContentStringFormat}"
                                              Foreground="{TemplateBinding Foreground}"
                                              FontFamily="{TemplateBinding FontFamily}"
                                              FontSize="{TemplateBinding FontSize}"
                                              FontStretch="{TemplateBinding FontStretch}"
                                              FontStyle="{TemplateBinding FontStyle}"
                                              FontWeight="{TemplateBinding FontWeight}" />
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter TargetName="border" Property="Background" Value="LightGray" />
                            </Trigger>
                            <Trigger Property="IsFocused" Value="True">
                                <Setter TargetName="border" Property="BorderBrush" Value="Blue" />
                            </Trigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter TargetName="border" Property="Opacity" Value="0.5" />
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>

    <Grid>
        <Label Style="{StaticResource CustomLabelStyle}" 
               Width="200" Height="50" Content="Styled Label" 
               Background="LightYellow" BorderBrush="Gray" 
               BorderThickness="2" Padding="5"
               HorizontalContentAlignment="Center" VerticalContentAlignment="Center" />
    </Grid>
</Window>

相关推荐

  1. WPF定义模板--Lable

    2024-07-10 06:04:06       12 阅读
  2. WPF定义模板--ToggleButton

    2024-07-10 06:04:06       9 阅读
  3. WPF定义控件模版

    2024-07-10 06:04:06       15 阅读
  4. C#--WPF定义控件模板示例

    2024-07-10 06:04:06       16 阅读
  5. WPF定义控件,聚合器模式传递消息

    2024-07-10 06:04:06       45 阅读
  6. WPF定义快捷命令

    2024-07-10 06:04:06       29 阅读
  7. Wpf-定义图标Button

    2024-07-10 06:04:06       21 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-10 06:04:06       4 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-10 06:04:06       4 阅读
  3. 在Django里面运行非项目文件

    2024-07-10 06:04:06       3 阅读
  4. Python语言-面向对象

    2024-07-10 06:04:06       3 阅读

热门阅读

  1. 自动化发布:Conda包依赖的持续集成之旅

    2024-07-10 06:04:06       12 阅读
  2. 探索Conda世界:使用conda list命令的全面指南

    2024-07-10 06:04:06       8 阅读
  3. Spark SQL----内置函数Aggregate Functions

    2024-07-10 06:04:06       8 阅读
  4. 掌握Conda配置术:conda config命令的深度指南

    2024-07-10 06:04:06       10 阅读
  5. 常见加密算法介绍

    2024-07-10 06:04:06       12 阅读
  6. Unity3D批量修改名称工具

    2024-07-10 06:04:06       10 阅读
  7. Istio在微服务中释放服务网格的力量

    2024-07-10 06:04:06       11 阅读
  8. js 回调函数如何追加参数

    2024-07-10 06:04:06       9 阅读
  9. Python文本数据可视化之“词云”图

    2024-07-10 06:04:06       10 阅读
  10. R语言学习笔记4-数据结构-矩阵

    2024-07-10 06:04:06       9 阅读
  11. 大模型日报 2024-07-09

    2024-07-10 06:04:06       8 阅读