将ObservableCollection绑定到ListView
我在使数据正确绑定方面遇到了很大的麻烦。 我已经从有类似问题的人那里阅读了大部分post,但由于某些原因我无法点击它。
我的表的XML是:
...
在我的代码中,我有:
public ObservableCollection _DocumentList = new ObservableCollection(); ... public ObservableCollection DocumentList{ get { return _DocumentList; } } ... public class Document { public string Description { get; set; } public string DateFiled { get; set; } public string UserFiledName { get; set; } public string Pages { get; set; } public string Notes { get; set; } public string Tag { get; set; } }
为了更新表,我使用:
_DocumentList.Add(new Document { Description = dr["Description"].ToString(), DateFiled = dr.GetDateTime(dr.GetOrdinal("DateFiled")).ToShortDateString(), UserFiledName = dr["UserFiledName"].ToString(), Pages = dr.GetInt32(dr.GetOrdinal("Pages")).ToString(), Notes = dr["Notes"].ToString(), Tag = dr["FileID"].ToString() });
似乎正在添加新项目,但listView上没有更新任何内容。
我已经阅读了这样的教程: http : //www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1
我已经尝试添加其他post中建议的所有通知代码。 没有什么对我有用。
我们将不胜感激。
而不是DataContext="{Binding DocumentList}"
尝试ItemsSource="{Binding DocumentList}"
。