ListView联动
<pre><code class="language-csharp">&lt;Grid&gt;
&lt;Grid.ColumnDefinitions&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;ColumnDefinition&gt;&lt;/ColumnDefinition&gt;
&lt;/Grid.ColumnDefinitions&gt;
&lt;ListView Grid.Column=&quot;0&quot; x:Name=&quot;listview&quot; SelectionChanged=&quot;listview_SelectionChanged&quot;&gt;
&lt;ListView.View&gt;
&lt;GridView&gt;
&lt;GridView.Columns&gt;
&lt;GridViewColumn Header=&quot;编号&quot; DisplayMemberBinding=&quot;{Binding Id}&quot;&gt;&lt;/GridViewColumn&gt;
&lt;GridViewColumn DisplayMemberBinding=&quot;{Binding Name}&quot;&gt;
&lt;GridViewColumn.Header&gt;
&lt;StackPanel Orientation=&quot;Horizontal&quot;&gt;
&lt;Image Height=&quot;10&quot; Width=&quot;10&quot; Source=&quot;image/question.png&quot;&gt;&lt;/Image&gt;
&lt;TextBlock Text=&quot;姓名&quot;&gt;&lt;/TextBlock&gt;
&lt;/StackPanel&gt;
&lt;/GridViewColumn.Header&gt;
&lt;/GridViewColumn&gt;
&lt;GridViewColumn Header=&quot;爱好&quot; DisplayMemberBinding=&quot;{Binding Address}&quot;&gt;&lt;/GridViewColumn&gt;
&lt;/GridView.Columns&gt;
&lt;/GridView&gt;
&lt;/ListView.View&gt;
&lt;/ListView&gt;
&lt;StackPanel Grid.Column=&quot;1&quot;&gt;
&lt;StackPanel Orientation=&quot;Horizontal&quot; Margin=&quot;5 5 0 0&quot;&gt;
&lt;TextBlock Text=&quot;编号:&quot;&gt;&lt;/TextBlock&gt;
&lt;TextBlock x:Name=&quot;txtId&quot;&gt;&lt;/TextBlock&gt;
&lt;/StackPanel&gt;
&lt;StackPanel Orientation=&quot;Horizontal&quot; Margin=&quot;5 5 0 0&quot;&gt;
&lt;TextBlock Text=&quot;名称:&quot;&gt;&lt;/TextBlock&gt;
&lt;TextBlock x:Name=&quot;txtName&quot;&gt;&lt;/TextBlock&gt;
&lt;/StackPanel&gt;
&lt;StackPanel Orientation=&quot;Horizontal&quot; Margin=&quot;5 5 0 0&quot;&gt;
&lt;TextBlock Text=&quot;爱好:&quot;&gt;&lt;/TextBlock&gt;
&lt;TextBlock x:Name=&quot;txtAddress&quot;&gt;&lt;/TextBlock&gt;
&lt;/StackPanel&gt;
&lt;/StackPanel&gt;
&lt;/Grid&gt;
/// &lt;summary&gt;
/// ListViews.xaml 的交互逻辑
/// &lt;/summary&gt;
public partial class ListViews : Window
{
public ListViews()
{
InitializeComponent();
//listview.Items.Add(new Person { Id = 1, Name = &quot;张三&quot;, Address = &quot;1123456&quot; });
//listview.Items.Add(new Person { Id = 2, Name = &quot;lis&quot;, Address = &quot;1123456&quot; });
//listview.Items.Add(new Person { Id = 3, Name = &quot;wangw&quot;, Address = &quot;1123456&quot; });
//listview.Items.Add(new Person { Id = 4, Name = &quot;zhaoliu&quot;, Address = &quot;1123456&quot; });
//listview.Items.Add(new Person { Id = 5, Name = &quot;marry&quot;, Address = &quot;1123456&quot; });
//listview.Items.Add(new Person { Id = 6, Name = &quot;jack&quot;, Address = &quot;1123456&quot; });
//listview.Items.Add(new Person { Id = 7, Name = &quot;ss&quot;, Address = &quot;1123456&quot; });
var data = new List&lt;Person&gt;()
{
new Person { Id=1,Name=&quot;张三&quot;,Address=&quot;1123456&quot;},
new Person { Id = 2, Name = &quot;lis&quot;, Address = &quot;1123456&quot; },
new Person { Id = 3, Name = &quot;wangw&quot;, Address = &quot;1123456&quot; }
};
listview.ItemsSource = data;
}
private void listview_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (listview.SelectedItem is Person person)
{
txtId.Text = person.Id.ToString();
txtName.Text = person.Name.ToString();
txtAddress.Text = person.Address.ToString();
}
}
}</code></pre>