wpf中在style的template尋找ControlTemplate和DataTemplate的控件


一、WPF中的兩棵樹 
WPF中每個控件的Template都是由ControlTemplate構成,ControlTemplate包含了構成該控件的各種子控件,這些子控件就構成了VisualTree;而在我們可見的界面,所有搭建出整個程序UI的控件構成了LoginTree。VisualTree和LoginTree相互獨立,互相不可訪問,每中樹都有各自的方法來查找自己的子控件。 
二、尋找ControlTemplate中的控件 
首先,我們在資源中新建一個包含三個TextBox的ControlTemplate,把它賦值給一個UserControl對象;然后我們再在程序界面添加一個TextBox,在資源中引用之前把TextBox改成圓角風格的Style:

 

 1 <Window x:Class="_11_221.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         Title="MainWindow" Height="350" Width="525">
 5     <Window.Resources>
 6         <ControlTemplate x:Key="cTmp">
 7             <StackPanel Background="Orange">
 8                 <TextBox x:Name="textBox1" Margin="6"/>
 9                 <TextBox x:Name="textBox2" Margin="6,0"/>
10                 <TextBox x:Name="textBox3" Margin="6"/>
11             </StackPanel>
12         </ControlTemplate>
13         <Style BasedOn="{x:Null}" TargetType="{x:Type TextBox}" x:Key="tbstyle">
14             <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
15             <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
16             <Setter Property="BorderThickness" Value="1"/>
17             <Setter Property="Padding" Value="1"/>
18             <Setter Property="AllowDrop" Value="true"/>
19             <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
20             <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
21             <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
22             <Setter Property="Template">
23                 <Setter.Value>
24                     <ControlTemplate TargetType="{x:Type TextBox}">
25                         <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}"  SnapsToDevicePixels="true"
26                         CornerRadius="10">
27                             <TextBlock x:Name="textblck1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
28                         </Border>
29                         <ControlTemplate.Triggers>
30                             <Trigger Property="IsEnabled" Value="false">
31                                 <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
32                                 <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
33                             </Trigger>
34                         </ControlTemplate.Triggers>
35                     </ControlTemplate>
36                 </Setter.Value>
37             </Setter>
38         </Style>
39 
40     </Window.Resources>
41     <Grid>
42         <StackPanel Background="Yellow">
43             <UserControl x:Name="uc" Template="{StaticResource cTmp}" Margin="5"/>
44             <TextBox x:Name="tb"  Style="{StaticResource tbstyle}"/>
45             <Button Content="find" Width="120" Height="30" Click="Button_Click"/>
46         </StackPanel>    
47     </Grid>
48 </Window>

我們實現的效果是,點擊按鈕,分別從UserControl和TextBox中找到構成他們的ControlTemplate,然后找到子控件並進行相關操作: 
后台代碼:

 1 private void Button_Click(object sender, RoutedEventArgs e)
 2         {
 3             TextBox t = this.uc.Template.FindName("textBox1", this.uc) as TextBox;
 4             t.Text = "hello";
 5             StackPanel sp = t.Parent as StackPanel;
 6             (sp.Children[1] as TextBox).Text = "hello controltemplate";
 7             (sp.Children[2] as TextBox).Text = "find it";
 8 
 9             TextBlock tbl = this.tb.Template.FindName("textblck1", this.tb) as TextBlock;
10             tbl.Text = "in textbox";
11         }

ControlTemplate和DateTemplate都屬於Template,都可以給Template進行賦值,Template中提供了一個叫做FindName的接口,可以用來尋找模板中的控件。

三、尋找DataTemplate中的控件 
首先,先定義一個用於使用DataTemplate的類Student:

1 public class Student
2     {
3         public int Id { get; set; }
4         public string Name { get; set; }
5         public string Skill { get; set; }
6         public bool HasJob { get; set; }
7     }

XMAL代碼如下:

 

 1 <Window x:Class="_11_222.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:local="clr-namespace:_11_222"
 5         Title="MainWindow" Height="350" Width="525">
 6     <Window.Resources>
 7         <local:Student x:Key="stu" Id="1"  Name="Hyman" Skill="Linux" HasJob="True"/>
 8         <DataTemplate x:Key="stuDT">
 9             <StackPanel Orientation="Horizontal">
10                 <TextBox  Name="textbox1" Text="{Binding Id}"/>
11                 <TextBox  Name="textbox2" Text="{Binding Name}"/>
12                 <TextBox  Name="textbox3" Text="{Binding Skill}"/>
13             </StackPanel>
14         </DataTemplate>
15     </Window.Resources>
16     <Grid>
17         <StackPanel>
18             <ContentPresenter x:Name="cp" Content="{StaticResource stu}" ContentTemplate="{StaticResource stuDT}" />
19             <Button x:Name="button" Width="120" Height="30"  Click="button_Click" Content="find"/>
20         </StackPanel>
21     </Grid>
22 </Window>

實現find按鈕的處理函數,將找到的TextBox中的內容用MessageBox彈出:

1 private void button_Click(object sender, RoutedEventArgs e)
2         {
3             TextBox tb = this.cp.ContentTemplate.FindName("textbox2", this.cp) as TextBox;
4             MessageBox.Show(tb.Text);
5         }

界面效果如下: 
這里寫圖片描述 
這里寫圖片描述

 轉自:http://blog.csdn.net/hyman_c/article/details/52020310


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM