在正文開始之前需要介紹一個人:Sean Sexton. 來自明尼蘇達雙城的軟件工程師。最為出色的是他維護了兩個博客:2,000Things You Should Know About C# 和 2,000 Things You Should Know About WPF 。他以類似微博式的150字簡短語言來每天更新一條WPF和C#重要又容易被遺忘的知識。Follow他的博客也有一段日子了,很希望能夠分享給大家。
本系列我不僅會翻譯他的每一個tip,也會加入自己開發之中的看法和見解。本系列我希望自己也能和他一樣堅持下來,每天的進步才能促成偉大。
在這里鄭重說明.該系列是基於Sean Sexton先生的英文博客, Sean Sexton擁有全部版權和撤銷權利。
前文:<1-7> , <8-14>,<15-21>,<22-27>, <28-33>,<34-39>,<40-44>
[小九的學堂,致力於以平凡的語言描述不平凡的技術。如要轉載,請注明來源:小九的學堂。cnblogs.com/xfuture]
#45 Logical Tree 邏輯樹
WPF邏輯樹是用戶界面元素的層次結構關系的樹狀圖。如果你的UI是在Xaml里定義的,邏輯樹就是Xaml中元素整合為具有父子關系的樹的模型。它描述了在運行時這些元素之間的關系。邏輯樹可以幫助我們理解:
1. Resource lookup 資源查找
2. Property inheritance 屬性繼承
3. Event routing 路由事件。
下面有個例子:
<Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="A window.." Height="350" Width="525"> <StackPanel> <Button Content="Click Me" Height="23" HorizontalAlignment="Left" Width="75" Click="button1_Click" /> <TextBox /> <ListBox> <ListBoxItem Content="Barley"/> <ListBoxItem Content="Oats"/> </ListBox> </StackPanel> </Window>
邏輯樹的模型是:
#46 代碼中尋找邏輯樹
你可以使用LogicalTreeHelper.GetChildren方法來遍歷邏輯樹並列出所有在邏輯樹上的對象。
在邏輯樹上(LogicTree)上所有的元素都是DependencyObject, 你可以通過調用GetChildren方法獲得上層對象的所有子對象,並返回其集合。
// Enumerate each immediate child of main window. (Does NOT descend down tree) foreach (Object obj in LogicalTreeHelper.GetChildren(mainWindow as DependencyObject)) Debug.WriteLine(obj.ToString());
你可以通過嵌套循環來列出所有的子元素。
#47 查看邏輯樹小工具
下面介紹一個可以查看邏輯樹的小工具。
用法:將.XAML文件拖入窗口即可展示其邏輯樹。它內部機制是調用LogicalTreeHelper.GetChildren來獲得子元素並展示在一個treeview的控件上。
下載地址:DisplayWpfTrees.zip 源代碼:WPFLogicalTree project
該工具更詳細的介紹:An Application to Let You View WPF Logical Trees
#48 視覺樹(Visual tree)
WPF視覺樹打破了邏輯樹的框,深入到內部,展示了更低level的元素。在邏輯樹中的元素是XAML中一般的控件,而視覺樹中會顯示所有基本的視覺元素。所有在視覺樹上顯示的元素都繼承自Visual or Visual3D。
例子:
<Window x:Class="WpfApplication4.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml> <StackPanel> <Button Content="Click Me" /> <TextBox /> <ListBox> <ListBoxItem Content="Barley"/> <ListBoxItem Content="Oats"/> </ListBox> </StackPanel> </Window>
其視覺樹:
Window
Border
AdornerDecorator
ContentPresenter
StackPanel
Button
ButtonChrome
ContentPresenter
TextBlock
TextBox
ListBoxChrome
ScrollViewer
Grid
Rectangle
ScrollContentPresenter
TextBoxView
TextBoxLineDrawingVisual
AdornerLayer
Scrollbar
Scrollbar
ListBox
Border
ScrollViewer
Grid
Rectangle
ScrollContentPresenter
ItemsPresenter
VirtualizingStackPanel
ListBoxItem
Border
ContentPresenter
TextBlock
ListBoxItem
Border
ContentPresenter
TextBlock
AdornerLayer
ScrollBar
Scrollbar
AdornerLayer
#49 代碼中尋找視覺樹
你可以使用VisualTreeHelper.GetChildrenCount和GetChild方法來遍歷視覺樹並列出所有在視覺樹上的對象。
你可以使用循環來獲得所有的視覺樹上的元素:
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++) Debug.WriteLine(VisualTreeHelper.GetChild(obj, i));
#50 查看視覺樹小工具
下面介紹一個可以查看視覺樹的小工具。
用法:將.XAML文件拖入窗口即可展示其視覺樹。它內部機制是調用VisualTreeHelper.GetChildren來獲得子元素並展示在一個treeview的控件上。
下載地址:DisplayWpfTrees.zip 源代碼:WPFVisualTree project
更多信息:An Application to Let You View WPF Visual/Logical Trees
后篇會對WPF內部機制繼續做探索,敬請關注!
如果覺得有幫助,右下角贊一下吧~ (* *)