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

CollectionView绑定Persons列表后,无法识别Person的Name属性报错

问题排查与解决

你遇到的错误「The Name "Name" don't exist in the current context」,核心原因是Person类的定义不符合XAML绑定要求,结合细节问题,具体解决步骤如下:

1. 修正Person类的属性定义

你当前的Person类使用私有字段定义:

Person{ string name; int number};

XAML绑定只能访问公共属性,且属性名称区分大小写。你需要把私有字段改成公共属性,并且名称和绑定表达式一致:

public class Person
{
    public string Name { get; set; }
    public int Number { get; set; }
}

这里属性名用大写Name和Number,和你XAML中{Binding Name}、{Binding Person.Name}的绑定名称对应。

2. 确保Persons集合类型正确

为了让CollectionView在添加新数据后自动刷新UI,ViewModel里的Persons建议使用ObservableCollection<T>,而不是普通的List<T>:

public ObservableCollection<Person> Persons { get; set; } = new ObservableCollection<Person>();

3. 确认绑定上下文设置正确

检查你的页面是否已经将BindingContext指定为对应的ViewModel实例,比如在页面构造函数中:

public YourPage()
{
    InitializeComponent();
    BindingContext = new YourViewModel();
}

完成以上修改后,CollectionView的绑定应该就能正常识别Person的Name属性了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 07:52:39