WPF绑定问题
我有这个对象:
class a { public string Application; public DateTime From, To; }
我用它声明了这个清单:
ObservableCollection ApplicationsCollection = new ObservableCollection();
在我的XAML中我有:
当我做:
lstStatus.ItemsSource = ApplicationsCollection;
我收到一堆错误,列表视图中没有显示任何内容:
System.Windows.Data Error: 39 : BindingExpression path error: 'Application' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=Application; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 39 : BindingExpression path error: 'From' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=From; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String') System.Windows.Data Error: 39 : BindingExpression path error: 'To' property not found on 'object' ''a' (HashCode=60325168)'. BindingExpression:Path=To; DataItem='a' (HashCode=60325168); target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')
显然,对象具有类型a
和a显然具有正确的属性,所以为什么这不起作用?
看起来WPF无法直接绑定到字段,您必须使用如下属性:
class a { public string Application { get; set; } public DateTime From { get; set; } public DateTime To { get; set; } }
好的,你使用字段,但你需要属性
class a { public string Application { get;set; } public DateTime From { get;set; } public DateTime To { get;set; } }