一、WPF中的两棵树 
WPF中每个控件的Template都是由ControlTemplate构成,ControlTemplate包含了构成该控件的各种子控件,这些子控件就构成了VisualTree;而在我们可见的界面,所有搭建出整个程序UI的控件构成了LoginTree。VisualTree和LoginTree相互独立,互相不可访问,每中树都有各自的方法来查找自己的子控件。 
二、寻找ControlTemplate中的控件 
首先,我们在资源中新建一个包含三个TextBox的ControlTemplate,把它赋值给一个UserControl对象;然后我们再在程序界面添加一个TextBox,在资源中引用之前把TextBox改成圆角风格的Style:

 <Window x:Class="_11_221.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<ControlTemplate x:Key="cTmp">
<StackPanel Background="Orange">
<TextBox x:Name="textBox1" Margin=""/>
<TextBox x:Name="textBox2" Margin="6,0"/>
<TextBox x:Name="textBox3" Margin=""/>
</StackPanel>
</ControlTemplate>
<Style BasedOn="{x:Null}" TargetType="{x:Type TextBox}" x:Key="tbstyle">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
<Setter Property="BorderThickness" Value=""/>
<Setter Property="Padding" Value=""/>
<Setter Property="AllowDrop" Value="true"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TextBox}">
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="true"
CornerRadius="">
<TextBlock x:Name="textblck1" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.ControlBrushKey}}"/>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style> </Window.Resources>
<Grid>
<StackPanel Background="Yellow">
<UserControl x:Name="uc" Template="{StaticResource cTmp}" Margin=""/>
<TextBox x:Name="tb" Style="{StaticResource tbstyle}"/>
<Button Content="find" Width="" Height="" Click="Button_Click"/>
</StackPanel>
</Grid>
</Window>

我们实现的效果是,点击按钮,分别从UserControl和TextBox中找到构成他们的ControlTemplate,然后找到子控件并进行相关操作: 
后台代码:

 private void Button_Click(object sender, RoutedEventArgs e)
{
TextBox t = this.uc.Template.FindName("textBox1", this.uc) as TextBox;
t.Text = "hello";
StackPanel sp = t.Parent as StackPanel;
(sp.Children[] as TextBox).Text = "hello controltemplate";
(sp.Children[] as TextBox).Text = "find it"; TextBlock tbl = this.tb.Template.FindName("textblck1", this.tb) as TextBlock;
tbl.Text = "in textbox";
}

ControlTemplate和DateTemplate都属于Template,都可以给Template进行赋值,Template中提供了一个叫做FindName的接口,可以用来寻找模板中的控件。

三、寻找DataTemplate中的控件 
首先,先定义一个用于使用DataTemplate的类Student:

 public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Skill { get; set; }
public bool HasJob { get; set; }
}

XMAL代码如下:

 <Window x:Class="_11_222.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:_11_222"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<local:Student x:Key="stu" Id="" Name="Hyman" Skill="Linux" HasJob="True"/>
<DataTemplate x:Key="stuDT">
<StackPanel Orientation="Horizontal">
<TextBox Name="textbox1" Text="{Binding Id}"/>
<TextBox Name="textbox2" Text="{Binding Name}"/>
<TextBox Name="textbox3" Text="{Binding Skill}"/>
</StackPanel>
</DataTemplate>
</Window.Resources>
<Grid>
<StackPanel>
<ContentPresenter x:Name="cp" Content="{StaticResource stu}" ContentTemplate="{StaticResource stuDT}" />
<Button x:Name="button" Width="" Height="" Click="button_Click" Content="find"/>
</StackPanel>
</Grid>
</Window>

实现find按钮的处理函数,将找到的TextBox中的内容用MessageBox弹出:

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

界面效果如下: 
 

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

wpf中在style的template寻找ControlTemplate和DataTemplate的控件的更多相关文章

  1. wpf 中的style

    我们通常说的模板是用来参照的,同样在WPF中,模板是用来作为制作控件的参照. 一.认识模板 1.1WPF菜鸟看模板 前面的记录有提过,控件主要是算法和数据的载体.控件的算法主要体现在可以激发的事件.可 ...

  2. WPF 中的style 样式

    WPF相较于以前学的WinForm,WPF在UI设计与动画方面的炫丽是最吸引我来学习的.在WPF中XMAL代码的引入使得代码的编写能够前后端分离,为获得更好的界面,也使得我们不得不分出一半的时间花在前 ...

  3. 【WPF学习】第六十五章 创建无外观控件

    用户控件的目标是提供增补控件模板的设计表面,提供一种定义控件的快速方法,代价是失去了将来的灵活性.如果喜欢用户控件的功能,但需要修改使其可视化外观,使用这种方法就有问题了.例如,设想希望使用相同的颜色 ...

  4. C# WPF 低仿网易云音乐(PC)Banner动画控件

    原文:C# WPF 低仿网易云音乐(PC)Banner动画控件 由于技术有限没能做到一模一样的动画,只是粗略地做了一下.动画有点生硬,还有就是没做出网易云音乐的立体感.代码非常简单粗暴,而且我也写有很 ...

  5. WPF编程,通过Double Animation同时动态缩放和旋转控件的一种方法。

    原文:WPF编程,通过Double Animation同时动态缩放和旋转控件的一种方法. 版权声明:我不生产代码,我只是代码的搬运工. https://blog.csdn.net/qq_4330793 ...

  6. WPF中的Style(风格,样式)(转)

    在WPF中我们可以使用Style来设置控件的某些属性值,并使该设置影响到指定范围内的所有该类控件或影响指定的某一控件,比如说我们想将窗口中的所有按钮都保持某一种风格,那么我们可以设置一个Style,而 ...

  7. WPF中的Style

    一.Style基础知识 构成Style最重要的两种元素是Setter和Trigger Setter类帮助我们设置控件的静态外观风格 Trigger类帮助我们设置控件的行为风格 Setter类的Prop ...

  8. WPF中的Style(风格,样式)

    作者: 周银辉  来源: 博客园  发布时间: 2009-02-27 15:04  阅读: 6698 次  推荐: 0   原文链接   [收藏]   在WPF中我们可以使用Style来设置控件的某些 ...

  9. [WPF] 在 ViewModel 中让数据验证出错(Validation.HasError)的控件获得焦点

    1. 需求 在 MVVM 中 ViewModel 和 View 之间的交互通常都是靠 Icommand 和 INotifyPropertyChanged,不过有时候还会需要从 MVVM 中控制 Vie ...

随机推荐

  1. ASP.NET打开项目错误:将指定的计数添加到该信号量中会导致其超过最大计数。

    1.错误如图 2.解决方案 重启IIS即可,运行-> 输入IISRESET 命令 即可重启IIS,如图

  2. vue better-scroll 下拉上拉,加载刷新

    _initScroll(){             this.$nextTick(() => {                 if (!this.scroll) {             ...

  3. leetcode-50-pow()

    题目描述: 方法一: class Solution: def myPow(self, x: float, n: int) -> float: if n<0: x = 1/x return ...

  4. 启动web项目,监听器、过滤器、拦截器启动顺序

    启动顺序:监听器 > 过滤器 > 拦截器 记忆技巧:接到命令,监听电报,过滤敌情,拦截行动.

  5. 阿里云宣布 Serverless 容器服务 弹性容器实例 ECI 正式商业化

    摘要: 阿里云宣布弹性容器实例 ECI(Elastic Container Instance)正式商业化,ECI 是阿里云践行普惠的云计算理念,将 Serverless 和 Container 技术结 ...

  6. Eclipse中如何使用Hibernate

    首先创建一个java web项目,其目录如下: (1)创建文件夹hibernate4(用于存放下载的hibernate工具包lib/required文件夹下所有的jar包),jar包目录如下: (2) ...

  7. centos一些故障解决方法

    1. vmware下虚拟机centos,root登录时候提示鉴定故障解决方法 - lippor - 博客园 https://www.cnblogs.com/lippor/p/5537931.html ...

  8. VS2015遇到的自带报表的问题

    1.设计报表时候,没有“报表数据”工具,直接在报表设计那里按ALT+CTRL+D 2.设计报表时候有时候找不到这些按钮,只需要重置窗口布局就行了.

  9. Python sort()函数和sorted()

    1.原址排序 1)列表有自己的sort方法,其对列表进行原址排序,既然是原址排序,那显然元组不可能拥有这种方法,因为元组是不可修改的. truple无组报错: 2.副本排序 1)[:]分片方法 注意: ...

  10. 深夜Python - 第2夜 - 爬行

    深夜Python - 第2夜 - 爬行 我曾经幻想自己是一只蜗牛,有自己的一只小壳,不怕风,不怕雨,浪荡江湖,游历四方……夜猫兄一如既往地打断了我不切实际的幻想:“浪荡?游历?等你退休了都爬不出家门口 ...