10

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:

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

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.

And ideas would be appreciated.

Nathan Tornquist
  • 6,468
  • 10
  • 47
  • 72

1 Answers1

17

Instead of DataContext="{Binding DocumentList}" try ItemsSource="{Binding DocumentList}".

Bryan Walker
  • 871
  • 7
  • 9
  • That in combination with the DataContext missing from the tag that @nemesv brought up solved it. Thanks. – Nathan Tornquist May 18 '12 at 20:47
  • Up-vote his answer and accept it, @NathanTornquist. We love our Pavlovian feedback around here. – Yatrix May 18 '12 at 20:48
  • It wouldn't let me accept it yet. He answered too quickly. Don't worry though, I took care of it. – Nathan Tornquist May 18 '12 at 20:50
  • It looks like you're using the codebehind on your window for your data management. You might want to consider looking into an MVVM framework. It makes WPF much easier to work with once you get your head around the concepts. – Bryan Walker May 18 '12 at 20:59