<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. [Git] How to rename your remote branch

    Rename your local foo branch with bar: git branch -m foo bar Remember this will add the new branch w ...

  2. 数据库筛选用户,然后去掉一部分(列表求差),再随机返回一个用户。sqlalchemy + python集合(set) + random

    sqlalchemy和flask-sqlalchemy之间的东西不是太清晰. sqlalchemy文档太他妈多了.日. 今天遇到的实例. 用户进行随机匹配,系统随机返回一个一定筛选条件下的用户.为了用 ...

  3. Django之分页显示文章

    1.项目:http://www.cnblogs.com/jasonhaven/p/7493422.html 2.任务描述:页面分页显示文章 3.源代码 后台: from django.core.pag ...

  4. [Vue] Update Attributes, Classes and Styles in Vue.js with v-bind

    Use v-bind:class and v-bind:style to compute html classes and inline styles from component data. Vue ...

  5. web项目中配置多个数据源

    web项目中配置多个数据源 spring + mybatis 多数据源配置有两种解决方案 1.配置多个不同的数据源,使用一个sessionFactory,在业务逻辑使用的时候自动切换到不同的数据源,  ...

  6. java 线程排查问题流程

    1. 通过top命令查看当前系统CPU使用情况,定位CPU使用率超过100%的进程ID:2. 通过ps aux | grep PID命令进一步确定具体的线程信息:3. 通过ps -mp pid -o ...

  7. 【转】关于python中带下划线的变量和函数 的意义

    http://www.blogjava.net/lincode/archive/2011/02/02/343859.html 总结: 变量: 1.  前带_的变量:  标明是一个私有变量, 只用于标明 ...

  8. 【codeforces 750A】New Year and Hurry

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  9. 详细阐述Web开发中的图片上传问题

    Web开发中,图片上传是一种极其常见的功能.但是呢,每次做上传,都花费了不少时间. 一个"小功能"花费我这么多时间,真心不愉快. So,要得认真分析下原因. 1.在最初学习Java ...

  10. 子查询及exists

    子查询分为无关子查询和相关子查询 无关子查询,只查询一次,与外查询无关,可作为查询条件: select * from student where sno in (select sno from stu ...