“井水不犯河水”常用來形容兩個組織之間界限分明、互不相干,LogicTree與控件內部這顆小樹之間就保持着這種關系。換句話說,如果UI元素樹上有個X:Name=“TextBox1”的控件,某個控件內部也是由Template生成的x:Name="TextBox1"的控件,它們並不沖突,LogicTree不會看到控件內部的細節,控件內部元素也不會去理會控件外面是什么值。你可能會想:“這樣一來,萬一我想從控件外部訪問內部的控件,獲取它的屬性值,豈不是做不到了。”放心,WPF為我們准備了訪問控件內部小世界的入口,現在我們就開始出發尋找那些失落的控件。
由ControlTemplate和DataTemplate生成的控件都是“由Template生成的控件”。ControlTemplate和DataTemplate兩個類均派生自FrameWorkTemplate類,這個類有個名為FindName的方法供我們檢索其內部控件。也就是說,只要我們能拿到Template,找到其內部控件就不成問題。對於ControlTemplate對象,訪問其目標控件的Template屬性就可以拿到,但想拿到DataTemplate就要費一番周折了。千萬不要以為ListBoxItem或者ComBoxItem容器就是DataTemplate的目標控件哦!因為控件的Template和ContentTemplate完全是兩碼事。
我們先來尋找由ControlTemplate生成的控件。首先設計一個ControlTemplate並把它應用在一個UserControl控件上。界面上還有一個Button,在它的Click事件處理器中我們檢索ControlTemplate生成的代碼。
程序的XAML代碼如下:
- <Window x:Class="WpfApplication11.wnd11431"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="wnd11431" Height="174" Width="300">
- <Window.Resources>
- <ControlTemplate x:Key="cTemp">
- <StackPanel>
- <TextBox x:Name="txtBox1" BorderBrush="Black" Margin="5"></TextBox>
- <TextBox x:Name="txtBox2" BorderBrush="Black" Margin="5"></TextBox>
- <TextBox x:Name="txtBox3" BorderBrush="Black" Margin="5"></TextBox>
- </StackPanel>
- </ControlTemplate>
- </Window.Resources>
- <StackPanel>
- <UserControl x:Name="uc" Template="{StaticResource cTemp}" Margin="5"></UserControl>
- <Button Content="Find By Name" Width="200" Click="Button_Click"></Button>
- </StackPanel>
- </Window>
Button的事件處理器代碼如下:
- private void Button_Click(object sender, RoutedEventArgs e)
- {
- TextBox tb = uc.Template.FindName("txtBox1", uc) as TextBox;
- tb.Text = "TextBox1";
- StackPanel sp = tb.Parent as StackPanel;
- (sp.Children[1] as TextBox).Text = "TextBox2";
- (sp.Children[2] as TextBox).Text = "TextBox3";
- }

接下來我們來尋找由DataTemplate生成的控件。不過在正式尋找之前,我們先思考一個問題:尋找到一個由DataTemplate生成的控件之后,我們想從中獲取哪些數據,如果想單純獲取與用戶界面相關的數據(比如控件的高度、寬度等),這么做是正確的。但是如果是想獲取與業務邏輯相關的數據,那就要考慮是不是程序的設計出了問題------因為WPF采用的是數據驅動UI邏輯,獲取業務邏輯數據在底層就能做到,一般不會跑到表層來找。
DataTemplate最常用的地方就是GridViewColumn的CellTemplate屬性。把GridViewColumn放置在一個GridView控件里就可以生成表格了。GridViewColumn的默認CellTemplate是使用TextBlock只讀屬性顯示數據,如果我們想讓用戶能修改數據或者使用CheckBox顯示bool類型的數據的話就需要自定義DataTemplate了。
先定義Student的類:
- /// <summary>
- /// 數據結構
- /// </summary>
- public class Student
- {
- public int Id { get; set; }
- public string Name { get; set; }
- public string Skill { get; set; }
- public bool HasJob { get; set; }
- }
- <Window x:Class="WpfApplication11.wnd11432"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:local ="clr-namespace:WpfApplication11"
- xmlns:c="clr-namespace:System.Collections;assembly=mscorlib"
- Title="wnd11432" Height="250" Width="350">
- <Window.Resources>
- <!--數據集合-->
- <c:ArrayList x:Key="stuList">
- <local:Student Id="1" Name="Jack" Skill="C++" HasJob="true"></local:Student>
- <local:Student Id="2" Name="Tom" Skill="C#/Java" HasJob="true"></local:Student>
- <local:Student Id="3" Name="Super" Skill="SQL" HasJob="false"></local:Student>
- <local:Student Id="4" Name="Mac" Skill="WPF" HasJob="false"></local:Student>
- <local:Student Id="5" Name="Thinker" Skill="Writing" HasJob="true"></local:Student>
- <local:Student Id="6" Name="Pad" Skill="ASP.NET" HasJob="true"></local:Student>
- </c:ArrayList>
- <!--數據模版-->
- <DataTemplate x:Key="nameDT">
- <TextBox x:Name="txtBoxName" Width="100" Text="{Binding Name}" GotFocus="txtBoxName_GotFocus"></TextBox>
- </DataTemplate>
- <DataTemplate x:Key="skillDT">
- <TextBox x:Name="txtBoxSkill" Width="100" Text="{Binding Skill}"></TextBox>
- </DataTemplate>
- <DataTemplate x:Key="hasJobDT">
- <CheckBox x:Name="chBoxhasJob" Width="30" IsChecked="{Binding HasJob}"></CheckBox>
- </DataTemplate>
- </Window.Resources>
- <StackPanel Margin="5" Background="LightSlateGray">
- <ListView x:Name="listView" ItemsSource="{StaticResource stuList}">
- <ListView.View>
- <GridView>
- <GridView.Columns>
- <GridViewColumn Header="Id" Width="30" DisplayMemberBinding="{Binding Id}"></GridViewColumn>
- <GridViewColumn Header="Name" CellTemplate="{StaticResource nameDT}"></GridViewColumn>
- <GridViewColumn Header="Skill" CellTemplate="{StaticResource skillDT}"></GridViewColumn>
- <GridViewColumn Header="HasJob" CellTemplate="{StaticResource hasJobDT}"></GridViewColumn>
- </GridView.Columns>
- </GridView>
- </ListView.View>
- </ListView>
- </StackPanel>
- </Window>
- private void txtBoxName_GotFocus(object sender, RoutedEventArgs e)
- {
- // 業務邏輯數據
- TextBox tb = e.OriginalSource as TextBox;
- ContentPresenter cp = tb.TemplatedParent as ContentPresenter; // 數據模版目標控件
- Student stu = cp.Content as Student;
- listView.SelectedItem = stu; // 設置選中項
- // 訪問界面元素
- ListViewItem lvi = listView.ItemContainerGenerator.ContainerFromItem(stu) as ListViewItem; // 條目數據包裝
- CheckBox chb = FindVisualChild<CheckBox>(lvi);
- if(chb != null)
- {
- MessageBox.Show(string.Format("ChectBox IsChecked: {0}", chb.IsChecked.ToString()));
- }
- }
- private ChildType FindVisualChild<ChildType>(DependencyObject obj)
- where ChildType:DependencyObject
- {
- for(int i=0; i<VisualTreeHelper.GetChildrenCount(obj); i++)
- {
- DependencyObject child = VisualTreeHelper.GetChild(obj, i);
- if(child != null && child is ChildType)
- {
- return (child as ChildType);
- }
- else
- {
- ChildType childOfChild = FindVisualChild<ChildType>(child);
- if (childOfChild != null)
- return (childOfChild);
- }
- }
- return (null);
- }
txtBoxName_GotFocus的后半部分則借助 VisualTreeHelper類檢索由DataTemplate生成的控件。前面說過,每個ItemsControl的派生類(如ListBox,ComBox,ListView)都具有自己獨特的條目容器,本例中是一個包裝着Student對象的ListViewItem(注意,此ListViewItem對象的Content也是Student對象)。可以把這個ListViewItem控件視為一顆樹的根,使用VisualTreeHelper類就可以遍歷它的各個節點。本例中是吧遍歷算法分裝在了FindVisualChild泛型方法里。
運行程序,並單擊某個顯示姓名的TextBox,效果如下圖所示:
由本例可以看出,無論是從事件源頭“自下而上”的找,還是使用ItemContainerGenerator.ContainerFromItem方法找到條目容器再“自上而下”的找,總之,找到業務邏輯數據(Student實例)並不難,而工作中大多是操作業務邏輯數據。如果真的想找由DataTemplate生成的控件,對於結構簡單的控件,可以使用DataTemplate對象的FindName方法;對於結構復雜的控件,則需要借助VisualTreeHelper來實現。
可以使用WPF Inspector查看VisualTree或LogicTree細節: