WPF Label 控件全面教程:从基础到高级用法
<Window x:Class="WPF之Label.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WPF之Label"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Label x:Name="lb_test" Content="用户名" Width="100" Height="100"
FontSize="20" HorizontalContentAlignment="Center"></Label>
<Label>
<Image Source="C:\Users\Jumy\Downloads\线.png" Height="32" Width="20"></Image>
</Label>
</StackPanel>
</Window>
Label 是 WPF 中用于显示静态文本或绑定数据的轻量级控件,通常用于为其他控件(如 TextBox、ComboBox)提供说明性标签。相比 TextBlock,Label 支持内容对齐、快捷键绑定(访问键)和内容模板化,适合需要交互或复杂布局的场景。本教程将带你从基础用法到高级技巧,全面掌握 Label 的核心能力。
一、Label 基础用法
1. 基本属性
- Content:设置显示的文本或内容(支持任意对象)。
- Target:绑定到另一个控件(如 TextBox),实现访问键(Alt+快捷键)聚焦。
- Horizontal/VerticalContentAlignment:控制内容对齐方式。
- FontWeight/FontSize/Foreground:设置字体样式。
<Window x:Class="WpfApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Label 基础示例" Height="300" Width="400">
<StackPanel Margin="20">
<!-- 简单文本标签 -->
<Label Content="用户名:" FontWeight="Bold" Margin="0,0,0,10"/>
<!-- 带访问键的标签(Alt+U 聚焦到 TextBox) -->
<Label Content="_用户名:" Target="{Binding ElementName=userNameTextBox}" Margin="0,0,0,10"/>
<TextBox x:Name="userNameTextBox" Width="200" Height="30" Margin="0,0,0,20"/>
<!-- 自定义内容(支持任意对象) -->
<Label Margin="0,0,0,10">
<StackPanel Orientation="Horizontal">
<Image Source="icon.png" Width="16" Height="16" Margin="0,0,5,0"/>
<TextBlock Text="带图标的标签"/>
</StackPanel>
</Label>
<!-- 内容对齐 -->
<Label Content="右对齐文本" HorizontalContentAlignment="Right" Width="200" Margin="0,0,0,10"/>
</StackPanel>
</Window>
2. 访问键(AccessKey)
- 在
Content中通过下划线_定义快捷键(如_用户名对应 Alt+U)。 - 需通过
Target属性绑定到目标控件(如 TextBox)。
二、Label 高级功能
1. 数据绑定
Label 的 Content 可以绑定到 ViewModel 的属性,实现动态文本显示。
<!-- XAML -->
<Label Content="{Binding WelcomeMessage}" FontSize="16" Margin="0,0,0,20"/>
// ViewModel 示例
public class MainViewModel : INotifyPropertyChanged
{
private string _welcomeMessage;
public string WelcomeMessage
{
get => _welcomeMessage;
set
{
_welcomeMessage = value;
OnPropertyChanged();
}
}
public MainViewModel()
{
WelcomeMessage = "欢迎使用 WPF 应用!";
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
2. 内容模板化
通过 ContentTemplate 自定义复杂内容的显示方式。
<Label Content="{Binding User}" Margin="0,0,0,20">
<Label.ContentTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Ellipse Width="16" Height="16" Fill="Blue" Margin="0,0,5,0"/>
<TextBlock Text="{Binding Name}" FontWeight="Bold"/>
</StackPanel>
</DataTemplate>
</Label.ContentTemplate>
</Label>
// 数据模型示例
public class User
{
public string Name { get; set; } = "张三";
}
3. 样式与触发器
通过 Style 和 Trigger 实现动态样式变化。
<Window.Resources>
<Style x:Key="HighlightLabel" TargetType="Label">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="FontWeight" Value="Bold"/>
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<StackPanel>
<Label Content="鼠标悬停高亮" Style="{StaticResource HighlightLabel}" Margin="0,0,0,20"/>
</StackPanel>
三、Label 与 TextBlock 的对比
| 特性 | Label | TextBlock |
|---|---|---|
| 用途 | 控件标签、访问键绑定 | 静态文本显示 |
| 内容支持 | 任意对象(如 Image、StackPanel) | 仅文本或简单格式化文本 |
| 访问键 | 支持(通过 _ 和 Target) | 不支持 |
| 性能 | 较重(支持交互) | 更轻量(纯文本显示) |
| 模板化 | 支持 ContentTemplate | 支持 Inline 和 Run |
选择建议:
- 需要访问键或复杂内容时,使用 Label。
- 仅需显示静态文本时,优先使用 TextBlock(性能更好)。
四、Label 高级应用场景
1. 表单布局
结合 Grid 或 StackPanel 实现表单对齐。
<Grid Width="300" Margin="0,0,0,20">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Label Content="用户名:" Grid.Column="0" Grid.Row="0" Margin="0,0,10,5"/>
<TextBox Grid.Column="1" Grid.Row="0"/>
<Label Content="密码:" Grid.Column="0" Grid.Row="1" Margin="0,0,10,5"/>
<PasswordBox Grid.Column="1" Grid.Row="1"/>
</Grid>
2. 动态内容切换
通过 DataTrigger 或 VisualStateManager 动态切换内容。
<Label Content="{Binding Status}">
<Label.Style>
<Style TargetType="Label">
<Setter Property="Foreground" Value="Black"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Status}" Value="Error">
<Setter Property="Foreground" Value="Red"/>
<Setter Property="Content" Value="错误:请检查输入!"/>
</DataTrigger>
<DataTrigger Binding="{Binding Status}" Value="Success">
<Setter Property="Foreground" Value="Green"/>
<Setter Property="Content" Value="操作成功!"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Label.Style>
</Label>
3. 国际化支持
通过资源字典实现多语言文本切换。
<!-- 资源字典(Resources.xaml) -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<sys:String x:Key="UsernameLabel">用户名</sys:String>
<sys:String x:Key="PasswordLabel">密码</sys:String>
</ResourceDictionary>
<!-- 使用资源 -->
<Label Content="{DynamicResource UsernameLabel}" Margin="0,0,0,10"/>
<Label Content="{DynamicResource PasswordLabel}" Margin="0,0,0,10"/>
五、总结
- 核心功能:
- 支持静态文本、访问键绑定和复杂内容。
- 支持数据绑定和模板化。
- 适合表单标签、动态内容显示等场景。
- 高级技巧:
- 通过
ContentTemplate自定义内容布局。 - 使用
Style和Trigger实现动态样式。 - 结合
Grid或StackPanel实现表单对齐。
- 通过
- 扩展学习:
- 学习
TextBlock的轻量级文本显示用法。 - 掌握
DataTrigger和VisualStateManager的动态交互。 - 了解资源字典和国际化支持的实现方式。
- 学习
通过掌握 Label 的高级用法,你可以轻松创建结构清晰、交互丰富的 WPF 界面。建议结合实际项目需求,灵活运用样式和模板!
1702

被折叠的 条评论
为什么被折叠?



