简单的 WPF 4 问题:对象列表的 DataGrid 未显示任何记录
这让我抓狂。我在 WPF 中有一个数据网格,它在运行时不会填充记录。
这是 XAML。我以前在 Silverlight 中做过这个,尽管我是新手并且不太清楚缺少什么。
<DataGrid AutoGenerateColumns="True" Height="335" HorizontalAlignment="Left" Name="DataGrid1" VerticalAlignment="Top" Width="753">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Width="175" Binding="{Binding DateTimePosted}"/>
<DataGridTextColumn Header="Name" Width="275" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Debit" Width="100" Binding="{Binding DebitAmout}" />
<DataGridTextColumn Header="Credit" Width="100" Binding="{Binding CreditAmount}" />
<DataGridTextColumn Header="Balance" Width="100" Binding="{Binding Balance}" />
</DataGrid.Columns>
</DataGrid>
在加载事件的代码中,我执行以下操作。
DataGrid1.DataContext = Facade.GetTransactions
这将返回一个 TransactionItems(自定义对象)列表,该列表具有与我在数据网格 XAML 中绑定的字段相匹配的公共属性。
我已检查并确保返回 74 条记录。
网格仍然显示为空白。我要疯了,因为我找不到一个例子来告诉我如何纠正这个问题。
WPF 4。
我尝试过 AutoGenerateColumns = True 和 = False...没有差异
建议
This is driving me nuts. I have a datagrid in WPF that is not populating with records at runtime.
Here is the XAML. I've done this before in Silverlight although I am a newbie and can't quite figure out what is missing.
<DataGrid AutoGenerateColumns="True" Height="335" HorizontalAlignment="Left" Name="DataGrid1" VerticalAlignment="Top" Width="753">
<DataGrid.Columns>
<DataGridTextColumn Header="Date" Width="175" Binding="{Binding DateTimePosted}"/>
<DataGridTextColumn Header="Name" Width="275" Binding="{Binding Name}"/>
<DataGridTextColumn Header="Debit" Width="100" Binding="{Binding DebitAmout}" />
<DataGridTextColumn Header="Credit" Width="100" Binding="{Binding CreditAmount}" />
<DataGridTextColumn Header="Balance" Width="100" Binding="{Binding Balance}" />
</DataGrid.Columns>
</DataGrid>
In code in the loading event I do the following.
DataGrid1.DataContext = Facade.GetTransactions
this returns a list of TransactionItems (custom object) that have public properties matching the fields I am binding in the datagrid XAML.
I've checked and ensured 74 records are being returned.
The grid is still showing up as blank. I'm going bonkers as I can't find an example that is showing me what I can do to correct this.
WPF 4.
I've tried AutoGenerateColumns = True and = False... no diff
Advice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您需要使用 ItemsSource 属性,而不是 DataContext 属性。
I think you need to use the ItemsSource property, not the DataContext property.