using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WpfApp1
{
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public string Skill { get; set; }
public bool HasJob { get; set; }
}
}
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
xmlns:c ="clr-namespace:System.Collections;assembly=mscorlib"
mc:Ignorable="d"
Title="MainWindow" Height="249.526" Width="552.607">
<Window.Resources>
<c:ArrayList x:Key="stuList">
<local:Student Id="1" Name="zs" Skill="wpf" HasJob="True"></local:Student>
<local:Student Id="2" Name="lisi" Skill="java" HasJob="False"></local:Student>
<local:Student Id="3" Name="ww" Skill="python" HasJob="True"></local:Student>
<local:Student Id="4" Name="zl" Skill="c++" HasJob="True"></local:Student>
</c:ArrayList>
<DataTemplate x:Key="nameDT">
<TextBox x:Name="txtName" Text="{Binding Name}" GotFocus="TxtName_GotFocus"></TextBox>
</DataTemplate>
<DataTemplate x:Key="skillDT">
<TextBox x:Name="txtSkill" Text="{Binding Skill}"></TextBox>
</DataTemplate>
<DataTemplate x:Key="hjDT">
<CheckBox x:Name="ckHasJob" IsChecked="{Binding HasJob}"></CheckBox>
</DataTemplate>
</Window.Resources>
<Grid>
<ListView Margin="5" ItemsSource="{StaticResource stuList}" x:Name="lv">
<ListView.View>
<GridView>
<GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" Width="100"></GridViewColumn>
<GridViewColumn Header="Name" CellTemplate="{StaticResource nameDT}" Width="100"></GridViewColumn>
<GridViewColumn Header="Skill" CellTemplate="{StaticResource skillDT}" Width="100"></GridViewColumn>
<GridViewColumn Header="HasJob" CellTemplate="{StaticResource hjDT}" Width="100"></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 WpfApp1
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void TxtName_GotFocus(object sender, RoutedEventArgs e)
{
TextBox textBox = e.OriginalSource as TextBox;
ContentPresenter cp = textBox.TemplatedParent as ContentPresenter;
Student stu = cp.Content as Student;
this.lv.SelectedItem = stu;
ListViewItem lvi = this.lv.ItemContainerGenerator.ContainerFromItem(stu) as ListViewItem;
CheckBox checkBox = FindVisualChild<CheckBox>(lvi);
MessageBox.Show(checkBox.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;
}
}
}