在Windows Forms(WinForm)開發中,常用Label
控件來顯示只讀文本;當我們轉向WPF進行開發時,用于顯示文本的常用控件則是TextBlock
。TextBlock
是WPF中非常輕量且靈活的文本呈現控件,相比Label
而言,TextBlock
有更豐富的格式化功能和更好的性能表現。本文將從WinForm到WPF的轉型角度,為您介紹TextBlock
在WPF中的應用場景和典型用法。
WinForm與WPF之比較
WinForm中的文本顯示控件
在WinForm中,如果要在界面上展示文本內容,通常使用以下控件:
Label
:用于顯示只讀文本,幾乎無格式化選項,可更改字體、顏色等基本屬性TextBox
WPF中的文本顯示控件
WPF中可以使用以下控件來顯示文本:
TextBlock
:輕量級文本顯示控件,支持豐富的文本格式化(如Run
、Span
等)Label
:WPF中也提供了Label
控件,但更多是兼容WinForm的用法;Label
通常會包含一個ContentPresenter
來顯示內容
在實際WPF開發中,推薦使用TextBlock
實現只讀文本的顯示,因為它具有更好的可擴展性和渲染性能。
TextBlock的基礎應用
在WPF中,使用TextBlock
最簡單的方式就是直接將要顯示的文本通過Text
屬性綁定到控件上。下面是一個演示示例,包括在XAML中的用法和后端C#代碼。
基本示例
<Window x:Class="AppTextBlock.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:AppTextBlock"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel Margin="20">
<!-- 使用TextBlock顯示靜態文本 -->
<TextBlock Text="這是一個簡單的TextBlock,用于顯示文本,類似于WinForm的Label"
FontSize="16"
FontWeight="Bold"
Foreground="Blue"/>
<!-- 多行文本示例 -->
<TextBlock TextWrapping="Wrap"
Margin="0,10,0,0">
這是一段較長的文本,開啟TextWrapping后,TextBlock會根據控件或者其容器的寬度自動換行,非常方便。與WinForm中的Label相比較,WPF中的TextBlock可以更靈活地進行文本渲染。
</TextBlock>
</StackPanel>
</Window>

核心要點
TextBlock
TextWrapping="Wrap"
TextBlock的高級用法
混合使用Run標簽
和WinForm中的Label
只能在一個文本元素中展示單一格式不同,WPF中TextBlock
可以結合Run
標簽來實現多段文字的不同格式。
<Window x:Class="AppTextBlock.Window1"
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:AppTextBlock"
mc:Ignorable="d"
Title="Window1" Height="450" Width="800">
<StackPanel>
<TextBlock>
<!-- 普通文本 -->
<Run Text="這是一段普通文本。" />
<!-- 加粗文本 -->
<Run Text="這段文字加粗顯示。" FontWeight="Bold" />
<!-- 彩色文本 -->
<Run Text="這段文字為紅色。" Foreground="Red" />
</TextBlock>
</StackPanel>
</Window>

文本換行與行高
<Window x:Class="AppTextBlock.Window2"
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:AppTextBlock"
mc:Ignorable="d"
Title="Window2" Height="450" Width="800">
<Grid>
<TextBlock TextWrapping="Wrap"
LineHeight="30"
LineStackingStrategy="BlockLineHeight">
<!-- 使用LineBreak手動換行 -->
第一行內容<LineBreak/>
第二行內容<LineBreak/>
第三行內容
</TextBlock>
</Grid>
</Window>

TextWrapping="Wrap"
LineHeight="30"
LineStackingStrategy="BlockLineHeight"
數據綁定示例
在WPF的MVVM模式下,TextBlock
可以通過Binding
來與ViewModel中的屬性關聯,從而實現數據驅動的視圖更新。下面是一個簡單的示例。
ViewModel代碼
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AppTextBlock
{
publicclass MainViewModel : INotifyPropertyChanged
{
privatestring _displayText;
publicstring DisplayText
{
get => _displayText;
set
{
_displayText = value;
OnPropertyChanged(nameof(DisplayText));
}
}
public MainViewModel()
{
// 構造函數中初始化數據
DisplayText = "Hello,WPF的TextBlock!";
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged(string propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
綁定到TextBlock
<Window x:Class="AppTextBlock.Window3"
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:AppTextBlock"
mc:Ignorable="d"
Title="Window3" Height="450" Width="800">
<Window.DataContext>
<local:MainViewModel/>
</Window.DataContext>
<Grid>
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBlock Text="{Binding DisplayText}"
FontSize="18"
FontWeight="Bold"
Foreground="Purple"/>
</StackPanel>
</Grid>
</Window>

這樣在程序運行時,如果MainViewModel
的DisplayText
屬性更新,UI上TextBlock
顯示的文本將會自動刷新。
TextBlock vs. WinForm的Label
| | |
---|
| | |
| | |
| | |
| | TextWrapping |
| | |
| | Visual Studio / Blend 設計器 |
結束語
從WinForm轉型到WPF中,TextBlock
是顯示只讀文本的理想選擇。與WinForm中常用的Label
控件相比,TextBlock
不僅性能更好,而且支持更豐富的格式化選擇。同時WPF本身的MVVM數據綁定模式也讓界面邏輯與數據邏輯更好地分離,實現真正的組件化與模塊化開發。
在實際項目中,如果需要顯示的文本內容很復雜,或者需要結合Run
、Span
等子元素實現多樣式展示,TextBlock
都是很好的選擇。希望本文能幫助您快速上手WPF的TextBlock
控件!
閱讀原文:https://mp.weixin.qq.com/s/2gTGSp_9rONfoubaB5klpQ
該文章在 2025/5/16 9:04:59 編輯過