<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. php 模拟post的新发现,重点在最后的新方法

    最近两天项目需要,由于服务器正在开发,客户端进度稍快一些,没有服务器进行联调.因此我重操旧业,用PHP快速的写了一些web页面,算是当测试桩程序了,七八个web接口,基本上5到6个小时搞定了.由于当前 ...

  2. xv6进程切换-swtch函数

    https://blog.csdn.net/Swartz2015/article/details/61615603 xv6进程切换-swtch函数 进程切换中由于需要保存当前进程的寄存器状态信息,又要 ...

  3. 小强的HTML5移动开发之路(51)——jquerymobile中改善页面访问速度

    在使用jQuery Mobile进行开发的时候可以选择单页模版和多页模版,在使用单页模版的时候从一个页面跳转到另一个页面的时候需要从服务器请求,用户会感到略有停顿.使用多页模版,可以改善页面跳转之间的 ...

  4. [Angular] Reactive Form -- FormControl & formControlName, FormGroup, formGroup & formGroupName

    First time dealing with Reactive form might be a little bit hard to understand. I have used Angular- ...

  5. php如何实现读取网易有道词典输出单词的xml格式并且转化为html形式

    php实现读取网易有道词典输出单词的xml格式并且转化为html形式 一.总结 一句话总结:将xml中的文件用preg_match_all读出来,然后组合成想要的数据 1.explode除了爆炸的意思 ...

  6. 前端调用百度API

    <!DOCTYPE html> <html> <head>         <meta charset="UTF-8">       ...

  7. [Angular] Difference between Providers and ViewProviders

    For example we have a component: class TodoList { private todos: Todo[] = []; add(todo: Todo) {} rem ...

  8. js进阶 9-5 js如何确认form的提交和重置按钮

    js进阶 9-5 js如何确认form的提交和重置按钮 一.总结 一句话总结: 1.这个并不好做:onsubmit 里面的代码必须返回false才能取消onsubmit方法的执行,所以,有return ...

  9. JSP 九大内置对象(转)

    九大对象: 内置对象(又叫隐含对象,有9个内置对象):不需要预先声明就可以在脚本代码和表达式中随意使用 out: javax.servlet.jsp.JspWriter类型,代表输出流的对象.作用域为 ...

  10. 【BZOJ 1033】 [ZJOI2008]杀蚂蚁antbuster(判断线段是否和圆相交)

    [题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1033 [题意] https://www.zybuluo.com/Jerusalem/n ...