有人可以告诉我为什么我的 WPF DataGrid 中没有显示任何数据,代码如下:
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300"
xmlns:my="http://schemas.microsoft.com/wpf/2008/toolkit"
>
<Grid>
<my:DataGrid Name="myDataGrid" ItemsSource="{Binding Customers}">
<my:DataGrid.Columns>
<my:DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<my:DataGridTextColumn Header="Name1" Binding="{Binding Name1}" />
</my:DataGrid.Columns>
</my:DataGrid>
</Grid>
</Window>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
IList<Customers> list = new List<Customers>();
list.Add(new Customers() { Name = "Name1", Name2 = "Name2" });
list.Add(new Customers() { Name = "Name1", Name2 = "Name2" });
list.Add(new Customers() { Name = "Name1", Name2 = "Name2" });
myDataGrid.DataContext = new Customers() { Name = "Name1", Name2 = "Name2" };
}
}
public class Customers
{
public string Name { get; set; }
public string Name2 { get; set; }
}