You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Wpf中ListBox选中项关联ContentControl绑定失效问题求助

问题根源

你的问题核心在于数据源绑定逻辑错误:

  • XAML中声明的MyFriends是一个空的Person实例,并非存储三个人员的集合
  • 后台代码直接给listBoxMyFriends赋值Person[]数组,导致ListBox的实际数据源和ContentControl绑定的空Person实例完全无关
  • ContentControl没有绑定到ListBox的选中项,而是绑定了一个空对象,自然无法显示信息
解决方案

统一数据源,并让ContentControl绑定到ListBox的选中项,具体修改如下:

修改后的完整代码

后台代码(MainWindow.xaml.cs)

using System.Collections.ObjectModel;
using System.Windows;

namespace WpfApp1
{
    public partial class MainWindow : Window
    {
        // 声明可绑定的集合属性
        public ObservableCollection<Person> MyFriends { get; set; }

        public MainWindow()
        {
            InitializeComponent();
            // 初始化人员集合
            MyFriends = new ObservableCollection<Person>
            {
                new Person("Bob", 99, "red"),
                new Person("John", 20, "green"),
                new Person("Mike", 30, "blue")
            };
            // 设置窗口DataContext为自身,让XAML能绑定到MyFriends属性
            DataContext = this;
        }
    }

    public class Person
    {
        public string PersonName { get; set; }
        public int PersonAge { get; set; }
        public string PersonColour { get; set; }

        public Person() { }

        public Person(string name, int age, string colour)
        {
            PersonName = name;
            PersonAge = age;
            PersonColour = colour;
        }

        public override string ToString()
        {
            return PersonName;
        }
    }
}

XAML代码(MainWindow.xaml)

<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"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <DataTemplate x:Key="DetailTemplate">
            <Border Width="300" Height="100" Margin="20"
                    BorderBrush="Black" BorderThickness="1" Padding="8">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition/>
                        <RowDefinition/>
                        <RowDefinition/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition/>
                        <ColumnDefinition/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Row="0" Grid.Column="0" Text="Name:"/>
                    <TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=PersonName}"/>
                    <TextBlock Grid.Row="1" Grid.Column="0" Text="Age:"/>
                    <TextBlock Grid.Row="1" Grid.Column="1" Text="{Binding Path=PersonAge}"/>
                    <TextBlock Grid.Row="2" Grid.Column="0" Text="Colour"/>
                    <TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Path=PersonColour}"/>
                </Grid>
            </Border>
        </DataTemplate>
    </Window.Resources>

    <StackPanel>
        <TextBlock FontFamily="Verdana" FontSize="11"
                   Margin="5,15,0,10" FontWeight="Bold">My Friends:</TextBlock>
        <!-- 绑定窗口的MyFriends集合,开启选中项与集合当前项同步 -->
        <ListBox Width="200" IsSynchronizedWithCurrentItem="True" Name="listBoxMyFriends"
                 ItemsSource="{Binding MyFriends}"/>
        <TextBlock FontFamily="Verdana" FontSize="11" Name="textBlockMyFriends"
                   Margin="5,15,0,5" FontWeight="Bold">Information:</TextBlock>
        <!-- 绑定ListBox的选中项,展示详情 -->
        <ContentControl Content="{Binding ElementName=listBoxMyFriends, Path=SelectedItem}"
                        ContentTemplate="{StaticResource DetailTemplate}"/>
    </StackPanel>
</Window>
补充说明
  • 使用ObservableCollection<Person>替代数组,集合元素变更时会自动通知UI更新,更适配WPF绑定场景
  • IsSynchronizedWithCurrentItem="True"可让ListBox选中项与集合CurrentItem同步,此时也可改用{Binding MyFriends/}语法绑定集合当前项
  • 设置DataContext = this是为了让XAML绑定能找到窗口类的MyFriends属性,也可通过资源绑定等其他方式实现

内容的提问来源于stack exchange,提问作者AIP

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 17:35:25