本文介绍了将 ObservableCollection 绑定到 ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在正确绑定数据时遇到了很多麻烦.我已经阅读了来自有类似问题的人的大部分帖子,但由于某种原因,我无法点击它.

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.

我的表的 XML 是:

The XML for my table is:

<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>

在我的代码中:

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()
});

似乎正确添加了新项目,但 listView 上没有任何更新.

New items seem to be getting added correctly, but nothing is updated on the listView.

我已经阅读了这样的教程:http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1

I have read through tutorials like this: 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.

和想法将不胜感激.

推荐答案

代替 DataContext="{Binding DocumentList}" 试试 ItemsSource="{Binding DocumentList}".

这篇关于将 ObservableCollection 绑定到 ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 22:56
查看更多