How to make a WPF DataGrid display a ViewModel collection with binding and a DataTemplate
Asked Answered
M

2

5

I simply wish to display the contents of some sort of list inside of a DataGrid. I am currently trying to use an ObservableCollection<> and a DataGrid, but that can change. How do I DataTemplate the list and display it?

The list is in a ViewModel, which has been set in Apps.xaml.cs as the DataSource for the MainWindow.xaml:

protected override void OnStartup(StartupEventArgs e)
{
    base.OnStartup(e);

    var window = new MainWindow();

    // Create the ViewModel to which 
    // the main window binds.
    var viewModel = new MainWindowViewModel();

    // Allow all controls in the window to 
    // bind to the ViewModel by setting the 
    // DataContext, which propagates down 
    // the element tree.
    window.DataContext = viewModel;

    window.Show();
}

Here is the current list:

    public ObservableCollection<PersonData> SearchResults { get; set; }
    

As for my xaml, though, I am rather lost -- I've tried fiddling around with binding and ItemsSource, and really have no idea how to use them here. Also, I suppose using a DataTemplate will be necessary, as I need to let it know somehow that the columns need to display certain properties of the PersonData, such as first and last name, location, and title. Any help is appreciated.

EDIT

More generally, how does one simply display a ViewModel list, collection, what have you, period?

Mara answered 6/7, 2012 at 19:15 Comment(0)
M
1

Ach. Failure to pre-study is fatal -- I did not implement INotifyPropertyChanged. Found out how to here: http://msdn.microsoft.com/en-us/library/ms743695

Mara answered 9/7, 2012 at 14:4 Comment(0)
M
12

Your basic DataGrid syntax should look something like this:

<DataGrid ItemsSource="{Binding SearchResults}"
          AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="First Name"  Binding="{Binding FirstName}"/>
        <DataGridTextColumn Header="Last Name" Binding="{Binding LastName}" />
    </DataGrid.Columns>
</DataGrid>

If you don't want to display your data in a DataGrid, you can use an ItemsControl.

The default ItemsControl will add all your items to a StackPanel, and draw each of them using a TextBlock bound to the .ToString() of the item. You can change how the ItemsControl displays the items by specifying your own ItemsPanelTemplate and ItemTemplate for it to use. For some examples, check out my blog post on WPF's ItemsControl.

Mossback answered 6/7, 2012 at 19:55 Comment(3)
Thanks, but it seems my program is determined to hate me. I went ahead and tried simplifying by converting the search results to a collection of strings as on your blog, and I used the first ItemsControl example there; nothing shows.Mara
@NathanBond It sounds like the DataContext is not set correctly then, or your SearchResults collection is not raising a PropertyChanged notification. I'd recommend the tool Snoop to help debug the problemMossback
yes, I found I had forgotten to include the INotifiyPropertyChanged interface. Thank you for your help.Mara
M
1

Ach. Failure to pre-study is fatal -- I did not implement INotifyPropertyChanged. Found out how to here: http://msdn.microsoft.com/en-us/library/ms743695

Mara answered 9/7, 2012 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.