Associate an ObservableCollection with a ListView

I have a huge amount of problems with proper data binding. I read most of the posts here from people with similar problems, but for some reason I just can't get it to click.

XML for my table:

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

Inside 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 added correctly, but nothing is updated in the listView.

I read the following tutorials: http://www.switchonthecode.com/tutorials/wpf-tutorial-using-the-listview-part-1

And I tried to add all the notification code that is suggested in other posts. Nothing works for me.

And ideas will be appreciated.

+6
source share
1 answer

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

+13
source

Source: https://habr.com/ru/post/916127/


All Articles