一、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. centos7使用iptables作为防火墙方法

    centos7使用iptables作为防火墙方法查看firewalld状态: systemctl status firewalld将centos7默认的firewalld停止,并将iptables作为 ...

  2. java内存模型和垃圾回收

    摘抄并用于自查 JVM内存模型 1. Java程序具体执行的过程: Java源代码文件(.java后缀)会被Java编译器编译为字节码文件(.class后缀) 由JVM中的类加载器加载各个类的字节码文 ...

  3. JavaScript 数组函数 map()

    JavaScript 数组函数 map() 学习心得 map()函数是一个数组函数: 它对数组每个原素进行操作,不对空数组进行操作: 不改变原本的数组,返回新数组: arr.map(function( ...

  4. 【学术篇】bzoj3262 陌上花开. cdq分治入门

    花儿们已经很累了-- 无论是花形.颜色.还是气味, 都不是为了给人们摆出来欣赏的, 更不是为了当做出题的素材的, 她们并不想自己这些属性被没有生命的数字量化, 并不想和其它的花攀比, 并无意分出个三六 ...

  5. 使用line-height垂直居中在安卓手机上效果不好

    前端实现单行垂直居中用的最多的方法可能就是line-height了吧.该属性在pc端和ios手机上效果都很好,可到了安卓手机,有很大几率发生文字上移的现象 知乎有人分析了导致这一现象的原因,Andro ...

  6. 列表中的index,extend,count方法

    列表中的index,extend,count方法 #_author:Administrator#date:2019/10/24#1.index方法l=['blue','red','white','bl ...

  7. DEDE采集时自动生成摘要和关键字

    1.修改 include/dedecollection.class.php //自动分析关键字和摘要 preg_match("/<meta[\s]+name=['\"]key ...

  8. 02-Nov-2017 07:11:56.475 信息 [http-nio-8080-exec-10] com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource. Initializing c3p0 pool...

    报错: 02-Nov-2017 07:11:56.475 信息 [http-nio-8080-exec-10] com.mchange.v2.c3p0.impl.AbstractPoolBackedD ...

  9. JAVA 文件的上传下载

    一.上传文件 1.使用 transferTo 上传 @ResponseBody @RequestMapping(value = "/file/upload") public Res ...

  10. JSF(JavaServer Faces)简介

    JavaServer Faces (JSF) 是一种用于构建Java Web 应用程序的标准框架(是Java Community Process 规定的JSR-127标准).它提供了一种以组件为中心的 ...