C# WPF入门学习主线篇(七)—— Label常见属性和事件
欢迎来到C# WPF入门学习系列的第七篇。在前面的文章中,我们已经探讨了WPF中的Button
和TextBox
控件的使用。今天,我们将深入了解WPF中的另一个常用控件——Label
。本文将详细介绍Label
的所有属性和事件,并通过示例代码展示如何在实际应用中使用这些功能。
一、Label的基础知识
Label
是WPF中的一个基本控件,用于显示文本。与TextBlock
不同,Label
控件还可以用于标签和访问键(即快捷键)的绑定。
Label的基本定义
我们先来看看一个简单的 Label
定义:
<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">
<Grid>
<Label Content="Hello, World!" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Grid>
</Window>
在这个示例中,我们定义了一个 Label
控件,其内容为“Hello, World!”,并将其水平和垂直居中对齐。
二、Label的常见属性
1. Content
Content
属性用于设置或获取 Label
中显示的内容。可以是字符串、数字、控件或任何其他对象。
<Label Content="Hello, World!" />
2. FontSize
FontSize
属性用于设置 Label
中文本的大小。
<Label Content="Hello, World!" FontSize="16" />
3. FontFamily
FontFamily
属性用于设置 Label
中文本的字体。
<Label Content="Hello, World!" FontFamily="Arial" />
4. FontWeight
FontWeight
属性用于设置 Label
中文本的粗细。
<Label Content