Why isn't my WPF Datagrid showing data?
Posted
by Edward Tanguay
on Stack Overflow
See other posts from Stack Overflow
or by Edward Tanguay
Published on 2009-03-23T10:51:28Z
Indexed on
2010/03/21
21:11 UTC
Read the original article
Hit count: 504
This walkthrough says you can create a WPF datagrid in one line but doesn't give a full example.
So I created an example using a generic list and connected it to the WPF datagrid, but it doesn't show any data.
What do I need to change on the code below to get it to show data in the datagrid?
ANSWER:
This code works now:
XAML:
<Window x:Class="TestDatagrid345.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:toolkit="http://schemas.microsoft.com/wpf/2008/toolkit"
xmlns:local="clr-namespace:TestDatagrid345"
Title="Window1" Height="300" Width="300" Loaded="Window_Loaded">
<StackPanel>
<toolkit:DataGrid ItemsSource="{Binding}"/>
</StackPanel>
</Window>
Code Behind:
using System.Collections.Generic;
using System.Windows;
namespace TestDatagrid345
{
public partial class Window1 : Window
{
private List<Customer> _customers = new List<Customer>();
public List<Customer> Customers { get { return _customers; }}
public Window1()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
DataContext = Customers;
Customers.Add(new Customer { FirstName = "Tom", LastName = "Jones" });
Customers.Add(new Customer { FirstName = "Joe", LastName = "Thompson" });
Customers.Add(new Customer { FirstName = "Jill", LastName = "Smith" });
}
}
}
© Stack Overflow or respective owner