WPF VisualTreeHelper的使用

<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; }
    }
}

原文地址:https://www.cnblogs.com/dxmfans/p/9434618.html