有人能告訴我爲什麼沒有數據在我的WPF的DataGrid被displaysed用下面的代碼: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; }
}
Ups,在我的代碼中,我確實將DataContext設置爲我的List。您在上面提出的第2點解決了問題,謝謝 – Bob 2010-11-19 09:24:11