WPF自定义模板--Lable

发布于:2024-07-10 ⋅ 阅读:(163) ⋅ 点赞:(0)

目录

属性:

外观属性

内容属性

布局属性

字体属性

其他属性

触发器属性

完整案例:


属性:

外观属性
  • 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>

网站公告

今日签到

点亮在社区的每一天
去签到