<Window x:Class="MyWpf.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:c="clr-namespace:System.Collections;assembly=mscorlib"
        xmlns:local="clr-namespace:MyWpf"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <c:ArrayList x:Key="ps">
            <local:Person Id="1" Name="zhangsan" HasJob="True" skill="wpf"/>
            <local:Person Id="2" Name="lisi" HasJob="False" skill="C#"/>
            <local:Person Id="3" Name="wangwu" HasJob="True" skill="wpf"/>
            <local:Person Id="4" Name="maliu" HasJob="False" skill="C#"/>
               
        </c:ArrayList>
        <DataTemplate x:Key="IdTemp">
            <TextBlock Text="{Binding Id}"></TextBlock>
        </DataTemplate>
        <DataTemplate x:Key="NameTemp">
            <TextBox Text="{Binding Name}" GotFocus="Name_GotFocus"></TextBox>
        </DataTemplate>
        <DataTemplate x:Key="SkillTemp">
            <TextBox Text="{Binding skill}"></TextBox>
        </DataTemplate>
        <DataTemplate x:Key="HasJobTemp">
            <CheckBox IsChecked="{Binding HasJob}" Name="checkboxHasJob"></CheckBox>
        </DataTemplate>
    </Window.Resources>
    <Grid>
        <ListView ItemsSource="{StaticResource ps}" Name="lv">
            <ListView.View>
                <GridView>
                    <GridViewColumn Width="100" Header="Id" CellTemplate="{StaticResource IdTemp}"></GridViewColumn>
                    <GridViewColumn  Width="100" Header="Name" CellTemplate="{StaticResource NameTemp}"></GridViewColumn>
                    <GridViewColumn  Width="100" Header="Skill" CellTemplate="{StaticResource SkillTemp}"></GridViewColumn>
                    <GridViewColumn  Width="100" Header="HasJob" CellTemplate="{StaticResource HasJobTemp}"></GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </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.Navigation;
using System.Windows.Shapes;

namespace MyWpf
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<Person> lst = new List<Person>()
            {
                new Person(){ Id=1, Name="zhangsan", HasJob=false, skill="C#"},
                new Person(){ Id=2, Name="lisi", HasJob=true, skill="C#"},
                new Person(){ Id=3, Name="wangwu", HasJob=false, skill="wpf"},
                new Person(){ Id=4, Name="maliu", HasJob=true, skill="C#"},
                new Person(){ Id=5, Name="zhaoqi", HasJob=false, skill="wpf"},
            };

        }

        private void Name_GotFocus(object sender, RoutedEventArgs e)
        {
            TextBox tb = e.OriginalSource as TextBox;
            ContentPresenter cp = tb.TemplatedParent as ContentPresenter;
            Person p = cp.Content as Person;
            lv.SelectedItem = p;
            ListViewItem lvi = lv.ItemContainerGenerator.ContainerFromItem(p) as ListViewItem;
            CheckBox cb = FindVisualChild<CheckBox>(lvi);
            MessageBox.Show(cb.Name);
        }

        private ChildType FindVisualChild<ChildType>(DependencyObject obj) where ChildType : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
            {
                DependencyObject child = VisualTreeHelper.GetChild(obj, i);
                if (child != null && child is ChildType)
                {
                    return child as ChildType;
                }
                else
                {
                    ChildType childOfChild = FindVisualChild<ChildType>(child);
                    if (childOfChild!=null)
                    {
                        return childOfChild;
                    }
                }
               
            }
            return null;

        }

    }

    public class Person
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string skill { get; set; }
        public bool HasJob { get; set; }
    }
}

WPF VisualTreeHelper的使用的更多相关文章

  1. WPF中的VisualTreeHelper

    VisualTreeHelper提供了一组WPF控件树模型,通过VisualTreeHelper可以遍历控件的树形结构,获取我们所需要的控件以及相应的属性: VisualTreeHelper提供了一下 ...

  2. WPF之ComboBox的VisualTreeHelper

    原文:WPF之ComboBox的VisualTreeHelper 用WPF的ComboBox控件的时候,需要用到TextChanged属性,但是这个属性属于TextBox控件,不用担心,ComboBo ...

  3. WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象

    原文:WPF利用VisualTreeHelper遍历寻找对象的子级对象或者父级对象 简介 本文将完整叙述我利用VisualTreeHelper实现题述功能的全部过程,想直接看函数实现的朋友可以跳到函数 ...

  4. wpf通过VisualTreeHelper找到控件内所有CheckBox和TextBox并做相应绑定

    #region CheckBox与TextBox绑定 Dictionary<CheckBox, TextBox> CheckTextBoxDic = new Dictionary<C ...

  5. WPF ContextMenu+VisualTreeHelper实现删除控件操作

    <UserControl  MouseRightButtonDown="UserControl_MouseRightButtonDown" >    <UserC ...

  6. WPF之VisualTreeHelper

     /// <summary>        /// </summary>        /// <typeparam name="T">< ...

  7. WPF中Visible设为Collapse时,VisualTreeHelper.GetChildrenCount为0

    今天遇到一个奇怪的问题, 在给一个控件内的子元素绑定事件时,失败. 发现原因是,这个控件初始化时Visible="Collapse",这时控件内的可视树就没有生成.导致绑定事件失败 ...

  8. 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>

    在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...

  9. Problem of saving images in WPF (RenderTargetBitmap)zz

      To save a visual to an image file need to use RenderTargetBitmap, detail is reference to Save and ...

随机推荐

  1. .NET Framework基础知识(四)(转载)

    .反射:是编程的读取与类型相关联的元数据的行为.通过读取元数据,可以了解它是什么类型以及类型的成员. 比如类中的属性,方法,事件等.所属命名空间System.Reflection. 例:using S ...

  2. Android 监听软键盘按键的三种方式

    前言: 我们在Android手机上面有时候会遇到监听手机软键盘按键的时候,例如:我们在浏览器输入url完毕后可以点击软键盘右下角的“Go”按键加载url页面:在点击搜索框的时候,点击右下角的searc ...

  3. ECharts.js 超简单入门(本质canvas)

    ECharts.js 超简单入门(本质canvas) 一.总结 一句话总结:echarts这些图标的本质都是canvas. 二.ECharts.js学习(一) 简单入门 EChart.js 简单入门 ...

  4. Spring学习笔记之六(数据源的配置)

    1.前言 上一篇博客分析了,Spring中实现AOP的两种动态代理的机制,以下这篇博客.来解说一下Spring中的数据源的配置.  2.DAO支持的模板类 Spring提供了非常多关于Dao支持的模板 ...

  5. KVM,QEMU核心分析

    现在的问题是学习虚拟化软件KVM相关实施原则.处理,的源代码的分析总结,,若有不对的地方,希望大家提出. 因为有一些代码结构图或者是架构图上传比較麻烦.所以博文都放在了自己的个人博客上.麻烦大家移步查 ...

  6. Java String对象的经典问题(转)

    public class StringTest { public static void main(String[] args) { String strA = "abc"; St ...

  7. 属性动画Property Animation

    ViewPropertyAnimation 使用方式:View.animate() 后跟 translationX() 等方法,动画会自动执行. 注意translationX实现是调用对应的set方法 ...

  8. CUDA线程协作之共享存储器“__shared__”&&“__syncthreads()”

    在GPU并行编程中,一般情况下,各个处理器都需要了解其他处理器的执行状态,在各个并行副本之间进行通信和协作,这涉及到不同线程间的通信机制和并行执行线程的同步机制. 共享内存"__share_ ...

  9. android模拟器上不了网的解决办法

    Android模拟器默认的地址是10.0.2.3,默认的DNS也是10.0.2.3,对于在家里上网学习Android的人来讲,一般电脑的IP都是192.168.1.100之类的,不在同一个网段.所以就 ...

  10. 科学的解决Http Token拦截器TokenInterceptor实现

    1.写在前面 在做项目的时候,有时对接口要求比较严谨.先介绍下情况. 我这边Http 方式采用的是 OKhttp+Retrofit 后台一共分为三种token,分别是实名token(accessTok ...