<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. RMAN之一:快速入门 分类: H2_ORACLE 2014-02-17 16:11 689人阅读 评论(0) 收藏

    1.数据导出基础 (1)创建datapump导出文件的目录对象并为相应用户授予权限. 出于安全考虑,不允许oracle用户直接在OS上进行文件的操作,而应通过directory对象指定. SQL> ...

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

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

  3. [Node.js] Build microservices in Node.js with micro

    micro is a small module that makes it easy to write high performance and asynchronous microservices ...

  4. php标准库中QplQueue队列如何使用?

    php标准库中QplQueue队列如何使用? 一.总结 1.new对象,然后通过enqueue方法和dequeue方法使用. 二.php标准库中QplQueue队列如何使用? 队列这种数据结构更简单, ...

  5. sparksql load/save

    java public class LoadAndSaveDemo { private static SparkConf conf = new SparkConf().setAppName(" ...

  6. Zero Downtime Upgrade of Oracle 10g to Oracle 11g Using GoldenGate — 1

    Source Database DB Name:        zwc Schemas:         HR,OE,PM Version:          10.2.0.4 RAC:       ...

  7. Android高度仿新浪微博引导页面

    在写这一篇文章之前,先向大家推荐一篇博文:http://blog.csdn.net/dawanganban/article/details/17297671 感谢这位博主,我在该博主的基础上完成了对新 ...

  8. PAT 1051-1060 题解

    浏览全部代码:请戳 本文谨代表个人思路,欢迎讨论;) 1051. Pop Sequence (25) 题意 给定 stack 的容量,给定数据的入栈顺序:从 1 开始的正整数序列,在允许随机的出栈操作 ...

  9. 冒泡排序 和 选择排序的 区别 python

    参考:https://www.cnblogs.com/banana201/p/4928733.html ## 冒泡排序法(Bubblesort) ## 所谓排序法,就是对一组无序的序列进行有序的排序( ...

  10. spring4+springmvc+hibernate4 demo

    来自 CSDN . 其实下面的更好:加入了maven集成.Spring4 MVC Hibernate4集成 下面也是一篇讲的很详细的文章: hibernate4无法保存数据 而自己遇到的hiberna ...