[WPF] 使用Grid与GridSplitter排版布局
前言
在開發應用程式時,一個很重要的工作項目就是設計使用者介面的排版布局。WPF中所提供的Grid控制項,讓開發人員擁有將版面分割為欄列交錯表格區域的能力。而開發人員在使用Grid控制項分割版面之後,還可以在版面中加入GridSplitter控制項,用以在執行期間提供使用者動態調整表格區域大小的功能。
本篇文章介紹使用Grid控制項與GridSplitter控制項,來設計幾個常見的基本排版布局,為自己留個紀錄也希望能幫助到有需要的開發人員。
一上二下佈局
上圖是一個一上二下的佈局樣式,MSDN網站採用這個佈局樣式來提供各種資訊內容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!--Definition--> < Grid.RowDefinitions > < RowDefinition Height="192" /> < RowDefinition /> </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition Width="256" /> < ColumnDefinition /> </ Grid.ColumnDefinitions > <!--Panel--> < Border Grid.Row="0" Grid.Column="0" Background="LightCoral" Grid.ColumnSpan="2" /> <!--<Border Grid.Row="0" Grid.Column="1" Background="LightSalmon" />--> < Border Grid.Row="1" Grid.Column="0" Background="LightYellow" /> < Border Grid.Row="1" Grid.Column="1" Background="LightGreen" /> |
完成這個佈局樣式可以透過Grid控制項,將版面切割為2欄2列的表格,並且合併第0列中的兩個欄。
1 2 3 4 | <!--Splitter--> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" /> < GridSplitter Grid.Row="1" Grid.Column="0" Background="Transparent" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" /> |
加入GridSplitter控制項:
*在第0列、第0欄表格區域下方,附加一個定義為Grid.ColumnSpan="2"的GridSplitter控制項,用以提供動態調整上下兩列表格區域高度的功能。
*在第1列、第0欄表格區域右方,附加一個GridSplitter控制項,用以提供動態調整下方左右兩欄表格區域寬度的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | < Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="1024" Height="768"> < Grid > <!--Definition--> < Grid.RowDefinitions > < RowDefinition Height="192" /> < RowDefinition /> </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition Width="256" /> < ColumnDefinition /> </ Grid.ColumnDefinitions > <!--Panel--> < Border Grid.Row="0" Grid.Column="0" Background="LightCoral" Grid.ColumnSpan="2" /> <!--<Border Grid.Row="0" Grid.Column="1" Background="LightSalmon" />--> < Border Grid.Row="1" Grid.Column="0" Background="LightYellow" /> < Border Grid.Row="1" Grid.Column="1" Background="LightGreen" /> <!--Splitter--> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" /> < GridSplitter Grid.Row="1" Grid.Column="0" Background="Transparent" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" /> </ Grid > </ Window > |
在完成加入這些Grid控制項、GridSplitter控制項之後,記得將GridSplitter控制項的背景顏色定義為透明色(Background="Transparent"),用以提供漂亮的排版布局。
一左二右佈局
上圖是一個一左二右的佈局樣式,Outlook採用這個佈局樣式來提供各種資訊內容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!--Definition--> < Grid.RowDefinitions > < RowDefinition /> < RowDefinition /> </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition Width="256" /> < ColumnDefinition /> </ Grid.ColumnDefinitions > <!--Panel--> < Border Grid.Row="0" Grid.Column="0" Background="LightCoral" Grid.RowSpan="2"/> < Border Grid.Row="0" Grid.Column="1" Background="LightSalmon" /> <!--<Border Grid.Row="1" Grid.Column="0" Background="LightYellow" />--> < Border Grid.Row="1" Grid.Column="1" Background="LightGreen" /> |
完成這個佈局樣式可以透過Grid控制項,將版面切割為2欄2列的表格,並且合併第0欄中的兩個列。
1 2 3 4 | <!--Splitter--> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.RowSpan="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" /> < GridSplitter Grid.Row="0" Grid.Column="1" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" /> |
加入GridSplitter控制項:
*在第0列、第0欄表格區域右方,附加一個定義為Grid.RowSpan="2"的GridSplitter控制項,用以提供動態調整左右兩欄表格區域寬度的功能。
*在第0列、第1欄表格區域下方,附加一個GridSplitter控制項,用以提供動態調整右方上下兩欄表格區域高度的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | < Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="1024" Height="768"> < Grid > <!--Definition--> < Grid.RowDefinitions > < RowDefinition /> < RowDefinition /> </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition Width="256" /> < ColumnDefinition /> </ Grid.ColumnDefinitions > <!--Panel--> < Border Grid.Row="0" Grid.Column="0" Background="LightCoral" Grid.RowSpan="2"/> < Border Grid.Row="0" Grid.Column="1" Background="LightSalmon" /> <!--<Border Grid.Row="1" Grid.Column="0" Background="LightYellow" />--> < Border Grid.Row="1" Grid.Column="1" Background="LightGreen" /> <!--Splitter--> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.RowSpan="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" /> < GridSplitter Grid.Row="0" Grid.Column="1" Background="Transparent" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" /> </ Grid > </ Window > |
在完成加入這些Grid控制項、GridSplitter控制項之後,記得將GridSplitter控制項的背景顏色定義為透明色(Background="Transparent"),用以提供漂亮的排版布局。
四分割佈局
上圖是一個四分割的佈局樣式,電視牆採用這個佈局樣式來提供各種資訊內容。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <!--Definition--> < Grid.RowDefinitions > < RowDefinition /> < RowDefinition /> </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition /> < ColumnDefinition /> </ Grid.ColumnDefinitions > <!--Panel--> < Border Grid.Row="0" Grid.Column="0" Background="LightCoral" /> < Border Grid.Row="0" Grid.Column="1" Background="LightSalmon" /> < Border Grid.Row="1" Grid.Column="0" Background="LightYellow" /> < Border Grid.Row="1" Grid.Column="1" Background="LightGreen" /> |
完成這個佈局樣式可以透過Grid控制項,將版面切割為2欄2列的表格。
1 2 3 4 | <!--Splitter--> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" /> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.RowSpan="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" /> |
加入GridSplitter控制項:
*在第0列、第0欄表格區域下方,附加一個定義為Grid.ColumnSpan="2"的GridSplitter控制項,用以提供動態調整上下兩列表格區域高度的功能。
*在第0列、第0欄表格區域右方,附加一個定義為Grid.RowSpan="2"的GridSplitter控制項,用以提供動態調整左右兩欄表格區域寬度的功能。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | < Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="1024" Height="768"> < Grid > <!--Definition--> < Grid.RowDefinitions > < RowDefinition /> < RowDefinition /> </ Grid.RowDefinitions > < Grid.ColumnDefinitions > < ColumnDefinition /> < ColumnDefinition /> </ Grid.ColumnDefinitions > <!--Panel--> < Border Grid.Row="0" Grid.Column="0" Background="LightCoral" /> < Border Grid.Row="0" Grid.Column="1" Background="LightSalmon" /> < Border Grid.Row="1" Grid.Column="0" Background="LightYellow" /> < Border Grid.Row="1" Grid.Column="1" Background="LightGreen" /> <!--Splitter--> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Bottom" Height="5" /> < GridSplitter Grid.Row="0" Grid.Column="0" Background="Transparent" Grid.RowSpan="2" HorizontalAlignment="Right" VerticalAlignment="Stretch" Width="5" /> </ Grid > </ Window > |
在完成加入這些Grid控制項、GridSplitter控制項之後,記得將GridSplitter控制項的背景顏色定義為透明色(Background="Transparent"),用以提供漂亮的排版布局。
原始碼下載
點此下載原始碼:GridLayoutSample.rar
期許自己~
能以更簡潔的文字與程式碼,傳達出程式設計背後的精神。
真正做到「以形寫神」的境界。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
2012-04-01 [Object-oriented] 相依性