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

WPF中XAML ListView绑定ObservableCollection报错:DataContext找不到成员

问题分析与解决方案

核心问题

你在XAML里直接给ListView添加了<ListViewItem>,这会导致两个关键问题:

  1. 手动添加的ListViewItem会覆盖ItemsSource自动生成的项,集合里的User对象无法被正常渲染
  2. 该ListViewItem的DataContext继承自ListView的上下文(即MainWindow),所以{Binding EntireName}会去MainWindow中查找该属性,自然找不到,触发报错

修复步骤

1. 修正XAML的ListView结构

移除手动添加的<ListViewItem>,改用ItemTemplate定义每个捐赠者项的布局,确保每个项的DataContext对应集合中的User对象:

<Window x:Class="DonorWallFrontend.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:DonorWallFrontend"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800"
        DataContext="{Binding RelativeSource={RelativeSource Self}}">

    <ListView HorizontalContentAlignment="Stretch" x:Name="donorList" ItemsSource="{Binding Donors}">
        <!-- 定义每个项的模板 -->
        <ListView.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="105" />
                        <RowDefinition Height="80" />
                        <RowDefinition Height="80" />
                        <RowDefinition Height="80" />
                        <RowDefinition Height="80" />
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                        <ColumnDefinition Width="1*"/>
                    </Grid.ColumnDefinitions>
                    
                    <Rectangle Grid.Row="1" Grid.Column="0" Fill="Purple" />
                    <Image Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" HorizontalAlignment="Center" 
                           Source="C:\Workspace\silver_nameplate.png" Width="180" Height="65" />
                    <!-- 此时DataContext为User对象,绑定EntireName正常 -->
                    <TextBlock x:Name="NameBlock" Text="{Binding EntireName}" Grid.Row="1" Grid.Column="0" 
                               HorizontalAlignment="Center" VerticalAlignment="Top" FontSize="15" Margin="0,21,0,0"/>
                    <!-- 如需显示捐赠等级,添加以下TextBlock -->
                    <TextBlock Text="{Binding DonorLevel}" Grid.Row="1" Grid.Column="1" 
                               HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="15"/>
                </Grid>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>
    
</Window>

2. 简化代码后置的DataContext设置

删除代码中的donorList.DataContext = Donors;行,因为Window已将自身设为DataContext,ListView的ItemsSource="{Binding Donors}"会自动绑定到MainWindow的Donors属性。

3. 可选:给User类添加属性变更通知(推荐)

如果后续需要修改User对象的属性并同步更新UI,User类需实现INotifyPropertyChanged接口:

using System.ComponentModel;

namespace DonorWallFrontend
{   
    public class User : INotifyPropertyChanged
    {
        private string _entireName;
        public string EntireName 
        { 
            get => _entireName; 
            set 
            {
                _entireName = value;
                OnPropertyChanged(nameof(EntireName));
            }
        }

        private string _donorLevel;
        public string DonorLevel 
        { 
            get => _donorLevel; 
            set 
            {
                _donorLevel = value;
                OnPropertyChanged(nameof(DonorLevel));
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    // MainWindow类保持不变(移除donorList.DataContext那行即可)
}

验证逻辑

修改后,ListView会自动遍历Donors集合中的每个User对象,每个DataTemplate的DataContext对应单个User实例,此时{Binding EntireName}能正确找到User类的属性,报错将被解决。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 14:44:55