为什么 WPF 4.0 中的 DataGrid 不显示数据?
所以我得到了 ObservableCollection 的 tmplist,其中我有一些元素。
我将它连接到 WPF 4.0 中 DataGrid 的 ItemsSource,但它没有显示任何内容。不添加任何列。如果我添加断点,我可以看到“ItemsSource 计数等于 tmplist 元素的数量,但列计数等于 0。为什么会这样?这是一些代码:
private ObservableCollection<Products> tmplist = new ObservableCollection<Products>();
public Products_view()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Products_view_Loaded);
}
void Produkty_widok_Loaded(object sender, RoutedEventArgs e)
{
grid.ItemsSource = tmplist;
}
和 xaml:
<DataGrid Name="grid" CurrentCellChanged="grid_CurrentCellChanged" CellEditEnding="grid_CellEditEnding" RowEditEnding="grid_RowEditEnding" AlternatingRowBackground="#FFA0D1EA" AlternationCount="1" Background="#FF64A0BE" RowBackground="White" />
So i Got a tmplist of ObservableCollection in which i got few elements.
I connect it to the ItemsSource of DataGrid in WPF 4.0 and it doesn't show anything. Doesn't add any colums. If I add breakpoint I can see that the "ItemsSource count equals quantity of tmplist elements, but the Columns count equals 0. Why is that? Here is some code:
private ObservableCollection<Products> tmplist = new ObservableCollection<Products>();
public Products_view()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Products_view_Loaded);
}
void Produkty_widok_Loaded(object sender, RoutedEventArgs e)
{
grid.ItemsSource = tmplist;
}
and xaml:
<DataGrid Name="grid" CurrentCellChanged="grid_CurrentCellChanged" CellEditEnding="grid_CellEditEnding" RowEditEnding="grid_RowEditEnding" AlternatingRowBackground="#FFA0D1EA" AlternationCount="1" Background="#FF64A0BE" RowBackground="White" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
tmplist 必须填充项目。
尝试在构造函数中添加一个项目以进行测试。
tmplist must be filled with items.
Try adding an item to it in constructor for testing purposes.
检查数据网格的 AutoGenerateColumns 属性是否设置为 true。
Check that the AutoGenerateColumns property of your datagrid is set to true.