C# 将 ObservableCollection 绑定到 ListView
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10659347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bind an ObservableCollection to a ListView
提问by Nathan Tornquist
I am having an immense amount of trouble getting my data to bind correctly. I have read most the posts on here from people with similar issues, but for some reason I just can't get it to click.
我在让我的数据正确绑定时遇到了很多麻烦。我已经阅读了来自有类似问题的人的大部分帖子,但由于某种原因,我无法点击它。
The XML for my table is:
我的表的 XML 是:
<Window ... DataContext="{Binding RelativeSource={RelativeSource Self}}" >
...
<ListView Height="124" HorizontalAlignment="Left" Margin="12,46,0,0" Name="listViewDocuments" VerticalAlignment="Top" Width="Auto" DataContext="{Binding DocumentList}">
<ListView.View>
<GridView>
<GridViewColumn Width="160" Header="Description" DisplayMemberBinding="{Binding Description}"/>
<GridViewColumn Width="160" Header="Date Filed" DisplayMemberBinding="{Binding DateFiled}"/>
<GridViewColumn Width="160" Header="Filed By" DisplayMemberBinding="{Binding UserFiledName}"/>
<GridViewColumn Width="150" Header="Page" DisplayMemberBinding="{Binding Pages}"/>
<GridViewColumn Width="150" Header="Notes" DisplayMemberBinding="{Binding Notes}"/>
<GridViewColumn Width="Auto" Header="" />
</GridView>
</ListView.View>
</ListView>
Within my code I have:
在我的代码中,我有:
public ObservableCollection<Document> _DocumentList = new ObservableCollection<Document>();
...
public ObservableCollection<Document> 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; }
}
In an attempt to update the table I use:
为了更新我使用的表:
_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()
});
New items seem to be getting added correctly, but nothing is updated on the listView.
新项目似乎被正确添加,但 listView 上没有任何更新。
I have read through tutorials like this: http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1
我已经阅读了这样的教程:http: //www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1
And I have tried adding all of the notification code that is suggested in other posts. Nothing is working for me.
我已经尝试添加其他帖子中建议的所有通知代码。没有什么对我有用。
And ideas would be appreciated.
和想法将不胜感激。
采纳答案by Bryan Walker
Instead of DataContext="{Binding DocumentList}"try ItemsSource="{Binding DocumentList}".
而不是DataContext="{Binding DocumentList}"尝试ItemsSource="{Binding DocumentList}"。

