XAML
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid Height="117" HorizontalAlignment="Left" Margin="43,12,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200"
ItemsSource="{Binding}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn
Header="Id" Binding="{Binding Id}"/>
<DataGridTextColumn
Header="Name" Binding="{Binding Name}"/>
</DataGrid.Columns>
</DataGrid>
*从我认为我读到的文件来看,这是什么 为收集和列中的每个项目做一个行 每个财产*
<DataGrid AutoGenerateColumns="True" Height="117" HorizontalAlignment="Left" ItemsSource="{Binding}" Margin="43,135,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="429">
</DataGrid>
</Grid>
</Window>
DATA
namespace WpfApplication1
{
public class Foo
{
public int Id { get; set; }
public string Name { get; set; }
}
class Data
{
public static IEnumerable<Foo> Foos
{
get
{
for (int i = 0; i < 5; i++)
{
yield return new Foo { Id = i, Name = "Foo" + i.ToString() };
}
}
}
}
}
INIT
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
dataGrid1.DataContext = Data.Foos;
dataGrid2.DataContext = Data.Foos; // corrected thanks to post/answer
}
}
RESULT
编辑从海报校正后(谢谢!)我有正确的行数但没有列。 (这是我在把这篇文章放在一起之前所看到的,如果我没有蠢蠢欲言,那将是原来的问题)
答案 0 :(得分:1)
编辑:
试试这个
dataGrid1.DataContext= new ObservableCollection<Foo>(Data.Foos);
dataGrid2.DataContext= new ObservableCollection<Foo>(Data.Foos);
这是ObservableCollection上的一些有用资料。