<Button x:Name="btn"Content="退出"Width=" 100"Height="25"Click="btn_Click" IsDefault="True"/>
<Button x:Name="btn" <!-- 控件標識:定義按鈕的實例名稱為"btn",用于后臺代碼(如C#)中引用該控件 -->Content="退出" <!-- 顯示文本:設置按鈕上顯示的文本內容為"退出" -->Width=" 100" <!-- 控件寬度:設置按鈕寬度為100設備無關單位(注意值前空格會被XAML自動忽略) -->Height="25" <!-- 控件高度:設置按鈕高度為25設備無關單位 -->Click="btn_Click" <!-- 事件綁定:將按鈕的點擊事件關聯到后臺代碼中的btn_Click方法 -->IsDefault="True"/> <!-- 默認行為:設置該按鈕為窗口默認按鈕,用戶按Enter鍵時會自動觸發點擊事件 -->
<WrapPanel><TextBlock Text="這是一個TextBlock文字塊" Margin="5"/><TextBlock Text="粗體文字" FontWeight="Bold" Margin="5"/><TextBlock Text="粗體文字" FontWeight="Light" Margin="5"/><TextBlock Text="斜體文字" FontStyle="Italic" Margin="5"/><TextBlock Text="微軟雅黑" FontFamily="Microsoft YaHei UI" Margin="5"/><TextBlock Text="大號字體" Foreground="Red" Margin="5"/><TextBlock Text="紅色文字" Foreground="Yellow" Margin="5"/><TextBlock Text="底色文字" Foreground="Yellow" Background="Red" Margin="5"/><TextBlock Background="LightGray" Height="25"><Run Foreground="Red">這行文字</Run><Run Foreground="Green">由三部分</Run><Run Foreground="Blue">組成</Run></TextBlock><Grid Width="150" Height="100" Margin="5" Background="LightGoldenrodYellow"><TextBlock Text="這段文字體現了文字的文本換行屬性TextWrapping" TextWrapping="Wrap" Margin="10"/></Grid><!--使用Run--><Grid><TextBlock x:Name="txtBlock"Width="320"Height="100"FontSize="15"FontFamily="微軟雅黑"FontWeight="Black"FontStretch="Condensed"Foreground="#dddddd"Background="Teal"TextAlignment="Center"TextWrapping="Wrap"TextTrimming="CharacterEllipsis"Margin="10" Padding="5"HorizontalAlignment="Left"VerticalAlignment="Center"LineHeight="30"ToolTip="《臨江仙·滾滾長江東逝水》"><Run Foreground="#CDB632" TextDecorations="Underline">滾滾長江東逝水,浪花淘盡英雄。是非成敗轉頭空。青山依舊在,幾度夕陽紅。</Run><Run Text="白發漁樵江渚上,慣看秋月春風。一壺濁酒喜相逢。古今多少事,都付笑談中。"/></TextBlock></Grid>
</WrapPanel>
一、基礎文本控件配置
- 基礎樣式組合
<TextBlock Text="粗體文字" FontWeight="Bold" Margin="5"/>
<TextBlock Text="斜體文字" FontStyle="Italic" Margin="5"/>
-
- 實現粗體/斜體效果,
Margin="5"
統一設置四邊5單位間距 - 注意:
FontWeight="Light"
在WPF中有效,但需確保字體支持該權重
- 實現粗體/斜體效果,
字體與顏色定制
<TextBlock Text="微軟雅黑" FontFamily="Microsoft YaHei UI"/>
<TextBlock Text="大號字體" Foreground="Red"/> <!-- 實際未設置FontSize屬性 -->
<TextBlock Text="紅色文字" Foreground="Yellow"/> <!-- 文本內容與顏色反諷設計 -->
- 支持中文字體名稱直接調用(需系統安裝對應字體)
- 警示案例:第四個控件
Background="Red"
與黃色前景形成高對比警示效果
二、進階排版技術
- 分段式文本(Run元素)
<TextBlock><Run Foreground="Red">這行文字</Run><Run Foreground="Green">由三部分</Run><Run Foreground="Blue">組成</Run>
</TextBlock>
-
- 允許同一TextBlock內分段設置樣式
- 支持混合使用
TextDecorations="Underline"
等修飾屬性
詩詞排版案例
<TextBlock LineHeight="30" TextAlignment="Center" TextTrimming="CharacterEllipsis"><Run Foreground="#CDB632" TextDecorations="Underline">滾滾長江東逝水...</Run><Run>白發漁樵江渚上...</Run>
</TextBlock>
LineHeight="30"
控制行距為2倍默認高度(基于15px字體)TextTrimming="CharacterEllipsis"
實現溢出文本以"..."省略ToolTip
屬性實現懸停顯示完整詩詞標題
三、布局與容器整合
- 自適應布局容器
<Grid Width="150" Height="100" Background="LightGoldenrodYellow"><TextBlock TextWrapping="Wrap" Margin="10"/>
</Grid>
-
TextWrapping="Wrap"
強制文本在容器邊界換行- 嵌套布局時,
Grid
容器通過明確尺寸約束文本域
擴展說明
- 顏色值規范
- 支持十六進制(
#CDB632
)、命名顏色(Red
)、系統資源(SystemColors
) - 推薦使用ARGB格式(如
#80FF0000
半透明紅)增強設計靈活性
- 支持十六進制(
- 排版精度控制
TextAlignment="Center"
實現水平居中VerticalAlignment="Center"
確保容器內垂直居中Padding="5"
在文本與容器邊界間添加緩沖空間
- 字體技術細節
FontStretch="Condensed"
壓縮字符寬度(需字體支持)微軟雅黑
在Windows系統默認安裝,跨平臺需字體回退策略
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"><TextBlock Text="用戶名" Margin="5"/><TextBox x:Name="txtbox" Width="100" Height="25" MaxLength="10" CharacterCasing="Upper"/><Button x:Name="btn" Content="確定" Height="25" Margin="5 0" Click="btn_Click"/></StackPanel>
<!-- 水平布局容器:創建水平排列的堆疊面板,在父容器中水平和垂直居中 -->
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center"><!-- 標簽控件:顯示"用戶名"文本,四邊保留5單位間距 --><TextBlock Text="用戶名" Margin="5"/><!-- 輸入框控件:設置輸入限制與格式規范 --><TextBox x:Name="txtbox" Width="100" Height="25"MaxLength="10" <!-- 最大字符數:限制輸入不超過10個字符 -->CharacterCasing="Upper"/> <!-- 自動轉換:強制所有輸入字母轉為大寫 --><!-- 功能按鈕:綁定點擊事件實現交互邏輯 --><Button x:Name="btn" Content="確定" Height="25"Margin="5 0" <!-- 邊距優化:左右無間距,上下保留5單位間距 -->Click="btn_Click"/> <!-- 事件綁定:關聯后臺btn_Click方法 -->
</StackPanel>
// 創建文本范圍對象:獲取RichTextBox從開頭到結尾的全部內容
TextRange txtRange = new TextRange(richTxtBox.Document.ContentStart, // 內容起始點(邏輯位置)richTxtBox.Document.ContentEnd // 內容結束點(包含最后一個字符后的邏輯位置)
);// 彈窗顯示純文本內容(自動過濾所有格式和控件)
MessageBox.Show(txtRange.Text); // 實際應用場景:文本導出預覽/字數統計 // 創建新段落容器(相當于HTML的<p>標簽)
Paragraph paragraph = new Paragraph(); // 默認帶段落間距,可通過Margin屬性調整 // 創建帶動態時間的文本片段(類似<span>標簽)
Run run = new Run($"當前時間:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
run.Foreground = Brushes.Black; // 設置字體顏色(等效于Foreground="#FF000000")// 將文本片段插入段落
paragraph.Inlines.Add(run); // 支持多Run組合實現彩色混排文本 // 將段落添加到富文本框文檔末尾
richTxtBox.Document.Blocks.Add(paragraph); // Blocks集合管理段落/表格等塊級元素
二、擴展技術說明(WPF富文本處理)
1. 文檔對象模型
對象 | 作用 |
---|---|
TextRange | 跨段落文本操作(支持格式復制/批量修改) |
Paragraph | 段落容器(可設置TextIndent 首行縮進/LineHeight 行高) |
Run | 基礎文本單元(最小樣式應用范圍,改變屬性會中斷樣式繼承) |
<StackPanel><RichTextBox x:Name="richTxtBox" Margin="10 5" Height="270"><FlowDocument><Paragraph>RichTextBox富文本框控件到底有什么強大的功能?<Bold Foreground="DarkRed">請看下面.</Bold></Paragraph><Paragraph Foreground="Blue">RichTextBox唯一的子元素是FlowDocument</Paragraph><Paragraph Foreground="DarkGreen">FlowDocument是指流文檔,一個流文檔由一個或多個Block構成,所以它有一個Blocks屬性。Block只是一個抽象基類,所以流文檔的子元素其實是繼承了Block的子類,例如:</Paragraph><List MarkerOffset="25" MarkerStyle="Decimal" StartIndex="1"><ListItem><Paragraph>BlockUIContainer(UI元素容器)</Paragraph></ListItem><ListItem><Paragraph>List(有序列表)</Paragraph></ListItem><ListItem><Paragraph>Paragraph(段落)</Paragraph></ListItem><ListItem><Paragraph>Section(分組)</Paragraph></ListItem><ListItem><Paragraph>Table(網格)</Paragraph></ListItem></List></FlowDocument></RichTextBox><Button x:Name="btn" Content="確定" Margin="10,5" Click="btn_Click"/>
</StackPanel>
<!-- 垂直堆疊容器:默認垂直排列子元素 -->
<StackPanel><!-- 富文本編輯器:支持復雜內容排版與交互 --><RichTextBox x:Name="richTxtBox" Margin="10 5" <!-- 邊距設置:左右10單位,上下5單位 -->Height="270"> <!-- 固定高度:適合顯示多段落內容 --><!-- 流式文檔容器:承載結構化富文本內容 --><FlowDocument><!-- 混合樣式段落:演示格式嵌套 --><Paragraph>RichTextBox富文本框控件到底有什么強大的功能?<Bold Foreground="DarkRed">請看下面.</Bold> <!-- 粗體+顏色復合樣式 --></Paragraph><!-- 帶顏色段落:說明核心概念 --><Paragraph Foreground="Blue">RichTextBox唯一的子元素是FlowDocument <!-- 重要約束條件 --></Paragraph><!-- 分層說明段落:解釋文檔結構 --><Paragraph Foreground="DarkGreen">FlowDocument是指流文檔,一個流文檔由一個或多個Block構成,所以它有一個Blocks屬性。Block只是一個抽象基類,所以流文檔的子元素其實是繼承了Block的子類,例如:</Paragraph><!-- 有序列表:展示Block派生類 --><List MarkerOffset="25" <!-- 編號縮進25單位 -->MarkerStyle="Decimal" <!-- 使用數字編號 -->StartIndex="1"> <!-- 起始編號為1 --><ListItem><Paragraph>BlockUIContainer(UI元素容器)</Paragraph> <!-- 可嵌入按鈕等控件 --></ListItem><ListItem><Paragraph>List(有序列表)</Paragraph> <!-- 當前列表自身的類型 --></ListItem><ListItem><Paragraph>Paragraph(段落)</Paragraph> <!-- 基礎文本容器 --></ListItem><ListItem><Paragraph>Section(分組)</Paragraph> <!-- 實現多列布局 --></ListItem><ListItem><Paragraph>Table(網格)</Paragraph> <!-- 創建表格數據 --></ListItem></List></FlowDocument></RichTextBox><!-- 功能按鈕:觸發后臺交互邏輯 --><Button x:Name="btn" Content="確定" Margin="10,5" <!-- 邊距設置:左右10單位,上下5單位 -->Click="btn_Click"/> <!-- 事件綁定:需在后臺實現處理方法 -->
</StackPanel>
<Button x:Name="btn2" Content="網站" Width="100" Height="30" Margin="5" Click="btn2_Click"><Button.ToolTip><StackPanel><TextBlock Text="官方網站" FontWeight="Bold"/><TextBlock Text="點擊這個按鈕,進入WPF中文網站"/><Border BorderBrush="Silver" BorderThickness="0 1 0 0" Margin="0 4"/><TextBlock Text="http://www.wpfsoft.com" FontStyle="Italic"/></StackPanel></Button.ToolTip></Button>
<!-- 網站導航按鈕:集成多層級工具提示 -->
<Button x:Name="btn2" Content="網站" Width="100" <!-- 標準尺寸:適配主流導航欄布局 -->Height="30"Margin="5" <!-- 邊距:四邊保留5單位間隔 -->Click="btn2_Click"> <!-- 事件綁定:需實現網頁跳轉邏輯 --><!-- 復合式工具提示:突破默認單行文本限制 --><Button.ToolTip><StackPanel><!-- 標題行:突出顯示網站性質 --><TextBlock Text="官方網站" FontWeight="Bold"/> <!-- 粗體強化重點 --><!-- 操作指引:說明按鈕核心功能 --><TextBlock Text="點擊這個按鈕,進入WPF中文網站"/><!-- 分隔線:使用邊框模擬水平分割線 --><Border BorderBrush="Silver" BorderThickness="0 1 0 0" <!-- 僅顯示上邊框 -->Margin="0 4"/> <!-- 上下增加4單位間距 --><!-- 網址展示:斜體弱化輔助信息 --><TextBlock Text="http://www.wpfsoft.com" FontStyle="Italic"/> <!-- 提示可復制性 --></StackPanel></Button.ToolTip>
</Button>