-1

Lets say I have the following classes. My intention is to bind the List to list view. This is just a sample code. Please help.

Class Data
{
public string name {get;set;}
public DateTime date{get;set;}
}

Class Items
{
//Code to get items from database and create a list<Data>...
ListViewDetails.DataContext=List<Data>; //Not Sure if this is the right way...
}

XAML:

 <ListView Name="ListViewDetails" Margin="4,20,40,100" ItemTemplate="{DynamicResource EmployeeTemplate}" ItemsSource="{Binding Path=List}">
    <GridView>

            <GridViewColumn Header="Employee Name" DisplayMemberBinding="{Binding Path=name}"/>

            <GridViewColumn Header="Hire Date" DisplayMemberBinding="{Binding Path=date}"/>
    </GridView>
    </ListView>
user2779848
  • 39
  • 2
  • 9

1 Answers1

1

No that is not correct:

  1. You don't want to set the data context on a control from code.
  2. You don't usually use List when binding

Instead:

  1. Set the data context for the whole page to a View Model object. You then bind to properties of this object, instead of setting the data context for the control.
  2. Use ObservableCollection<T> when binding to collections. If the collection has items added or removed, the UI will automatically update to reflect this.
BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
  • Thanks the idea really helped..It works..This is a perfect example..http://stackoverflow.com/questions/4891533/cant-get-wpf-listview-to-bind-to-observablecollection – user2779848 Sep 15 '14 at 18:11