<Window x:Class="Wpf180706.Window11"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:Wpf180706"
        Title="Window11" Height="300" Width="300">
    <Window.Resources>
        <local:Person x:Key="person" Id="1" Name="zhangsan" Skill="wpf"></local:Person>
        <DataTemplate x:Key="tpl">
            <Border BorderBrush="Orange" BorderThickness="3" CornerRadius="5">
                <StackPanel>
                    <TextBlock Text="{Binding Id}"></TextBlock>
                    <TextBlock Name="tb2" Text="{Binding Name}"></TextBlock>
                    <TextBlock Text="{Binding Skill}"></TextBlock>
                </StackPanel>
            </Border>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <StackPanel>
        <ContentPresenter Name="cp" Content="{StaticResource person}" ContentTemplate="{StaticResource tpl}"></ContentPresenter>
        <Button Click="btn_Click">btn</Button>
        </StackPanel>
    </Grid>

</Window>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Wpf180706
{
    /// <summary>
    /// Interaction logic for Window11.xaml
    /// </summary>
    public partial class Window11 : Window
    {
        public Window11()
        {
            InitializeComponent();
        }

        private void btn_Click(object sender, RoutedEventArgs e)
        {
           TextBlock tb =  (TextBlock)this.cp.ContentTemplate.FindName("tb2", this.cp);
           MessageBox.Show(tb.Text);
        }

    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Skill { get; set; }
    }
}

WPF 寻找数据模板中的元素的更多相关文章

  1. WPF 基础 - 在模板中找元素

    1. 在 ControlTemplate 中寻找元素 <Window.Resources> <ControlTemplate x:Key="cTmp"> & ...

  2. WPF数据模板中绑定事件不触发问题

    今天比较闲,做一个练手的项目,结果在xaml中写了一个用户的数据模板后,在其中的某个Canvas上绑定了一个鼠标左击的事件,结果调试的时候,无论怎么点击都不跳到断点那里,百思不得其解. 之后尝试不绑定 ...

  3. 继续聊WPF——动态数据模板

    我为啥称之为“动态数据模板”?先看看下面的截图,今天,我们就是要实现这种功能. 大概是这样的,我们定义的DataTemplate是通过触发器动态应用到 ComboBoxItem 上. 这个下拉列表控件 ...

  4. WPF 寻找控件模板中的元素

    <Window x:Class="Wpf180706.Window10"        xmlns="http://schemas.microsoft.com/wi ...

  5. 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中的元素

    [源码下载] 背水一战 Windows 10 (76) - 控件(控件基类): Control - 基础知识, 焦点相关, 运行时获取 ControlTemplate 和 DataTemplate 中 ...

  6. WPF教程十五:数据模板的使用(重发)

    数据模板 数据模板是一段如何显示绑定在VM对象的XAML代码.数据模板可以包含任意元素的组合,基于Binding来显示不同的信息. 在实际的开发中数据模板的应用场景很多,同样一个控件可以根据不同的绑定 ...

  7. 转载 WPF -- 控件模板 (ControlTemplate)(一) https://blog.csdn.net/qq_23018459/article/details/79899838

    ControlTemplate(控件模板)   https://blog.csdn.net/qq_23018459/article/details/79899838 WPF包含数据模板和控件模板,其中 ...

  8. dev Gridcontrol单元格值格式化及在模板中调用命令

    <dxg:GridColumn>                    <dxg:GridColumn.EditSettings>                        ...

  9. ListBox项模板中绑定ListBoxItem属性的方法

    原文:ListBox项模板中绑定ListBoxItem属性的方法 <ListBox> <ListBox.ItemTemplate> <DataTemplate> & ...

随机推荐

  1. mycat主从读写分离范例

    1.mycat二进制包安装 tar -zxvf Mycat-server-1.6.5-release-20180122220033-linux.tar.gzcd mycatmv mycat /opt/ ...

  2. 五一巨献,问答有礼,105QB送给IT互联网界的劳动人民

    活动主题:五一巨献,问答有礼,105QB送给IT互联网界的劳动人民活动时间:4月30日晚上10点~5月2日晚上10点活动期数:第1期,20150401 奖品:105QB获奖人数:20人1~5:每人10 ...

  3. Volley框架源代码分析

    Volley框架分析Github链接 Volley框架分析 Volley源代码解析 为了学习Volley的网络框架,我在AS中将Volley代码又一次撸了一遍,感觉这样的照抄代码也是一种挺好的学习方式 ...

  4. C#生成、解析xml文件以及处理报错原因

    转载自:http://blog.csdn.net/lilinoscar/article/details/21027319 简单的介绍一下生成XML文件以及解析,因为有些数据不一定放到数据库,减少链接数 ...

  5. (转自aierong原创技术随笔)sqlserver字符串拆分(split)方法汇总

    sqlserver字符串拆分(split)方法汇总   --方法0:动态SQL法declare @s varchar(100),@sql varchar(1000)set @s='1,2,3,4,5, ...

  6. Android推送进阶课程学习笔记

    今天在慕课网学习了Android进阶课程推送的server端处理回执的消息 . 这集课程主要介绍了,当server往client推送消息的时候,client须要发送一个回执回来确认收到了推送消息才算一 ...

  7. [React] Render Basic SVG Components in React

    React loves svg just as much as it loves html. In this lesson we cover how simple it is to make SVG ...

  8. php课程 6-20 字符串基础和去除空格和字符串填补函数

    php课程 6-20  字符串基础和去除空格和字符串填补函数 一.总结 一句话总结: 二.字符串 字符串定义:$str='hello world!'; 输出字符串:echo $str;print $s ...

  9. js如何遍历表单所有控件

    js如何遍历表单所有控件 一.总结 一句话总结: 1.获取form表单里面的的所有元素:通过formelement.elements,这里form元素通过name属性直接定位 var fele=for ...

  10. WPF入门(四)->线形区域Path内容填充之渐变色(LinearGradientBrush)

    原文:WPF入门(四)->线形区域Path内容填充之渐变色(LinearGradientBrush) 前面我们介绍到,Path对象表示一个用直线或者曲线连接的图形,我们可以使用Path.Data ...