DataGrid 不显示,ListView 工作正常吗?
认为我在这里缺少一些非常简单的东西,但我似乎无法让我的数据网格正确显示?
我最初使用 ListView 来显示一些表格数据,但我想自动调整列大小,因此我使用 DataGridTextColumns 切换到 DataGrid,但现在我什么也没得到。网格中显示正确的行数,但每个单元格显示为空白。有什么想法吗?
这是我的 XAML:
<DataGrid ItemsSource="{Binding Charges}" AutoGenerateColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Count" Binding="{Binding Count, Mode=OneWay}" Width="SizeToCells" />
<DataGridTextColumn Header="Charge" Binding="{Binding LawId, Mode=OneWay, Converter={StaticResource LawConverter}}" Width="SizeToCells" />
<DataGridTextColumn Header="Incident Date" Binding="{Binding IncidentDate, Mode=OneWay, Converter={StaticResource DateConverter}}" Width="SizeToCells" />
<DataGridCheckBoxColumn Header="Convicted" Binding="{Binding IsConvicted, Mode=OneWay}" />
<DataGridTextColumn Header="Disposition" Binding="{Binding ChargeDispositionCodeId, Mode=OneWay, Converter={StaticResource CodeConverter}, ConverterParameter='ChargeDispositionCode'}" Width="SizeToCells" />
<DataGridTextColumn Header="Disposition Date" Binding="{Binding DispositionDate, Mode=OneWay, Converter={StaticResource DateConverter}}" Width="SizeToCells" />
</DataGrid.Columns>
</DataGrid>
其他信息:
这个 xaml 确实可以正常工作并显示正确,但我希望列能够自动调整大小,因此 DataGrid 会更好:
<ListView ItemsSource="{Binding Charges}">
<ListView.View>
<GridView>
<GridViewColumn Header="Count" DisplayMemberBinding="{Binding Count, Mode=OneWay}" />
<GridViewColumn Header="Charge" DisplayMemberBinding="{Binding LawId, Mode=OneWay, Converter={StaticResource LawConverter}}" />
<GridViewColumn Header="Incident Date" DisplayMemberBinding="{Binding IncidentDate, Mode=OneWay, Converter={StaticResource DateConverter}}" />
<GridViewColumn Header="Convicted" DisplayMemberBinding="{Binding IsConvicted, Mode=OneWay}" />
<GridViewColumn Header="Disposition" DisplayMemberBinding="{Binding ChargeDispositionCodeId, Mode=OneWay, Converter={StaticResource CodeConverter}, ConverterParameter='ChargeDispositionCode'}" />
<GridViewColumn Header="Disposition Date" DisplayMemberBinding="{Binding DispositionDate, Mode=OneWay, Converter={StaticResource DateConverter}}" />
</GridView>
</ListView.View>
</ListView>
Think I'm missing something pretty simple here, but I can't seem to get my Datagrid to properly display?
I was originally using a ListView to display some tabular data, but I wanted to autosize the columns and so I switched over to a DataGrid using DataGridTextColumns and now I get nothing. The correct number of rows appears in the grid, but each cell appears to be blank. Any ideas?
Here's my XAML:
<DataGrid ItemsSource="{Binding Charges}" AutoGenerateColumns="True">
<DataGrid.Columns>
<DataGridTextColumn Header="Count" Binding="{Binding Count, Mode=OneWay}" Width="SizeToCells" />
<DataGridTextColumn Header="Charge" Binding="{Binding LawId, Mode=OneWay, Converter={StaticResource LawConverter}}" Width="SizeToCells" />
<DataGridTextColumn Header="Incident Date" Binding="{Binding IncidentDate, Mode=OneWay, Converter={StaticResource DateConverter}}" Width="SizeToCells" />
<DataGridCheckBoxColumn Header="Convicted" Binding="{Binding IsConvicted, Mode=OneWay}" />
<DataGridTextColumn Header="Disposition" Binding="{Binding ChargeDispositionCodeId, Mode=OneWay, Converter={StaticResource CodeConverter}, ConverterParameter='ChargeDispositionCode'}" Width="SizeToCells" />
<DataGridTextColumn Header="Disposition Date" Binding="{Binding DispositionDate, Mode=OneWay, Converter={StaticResource DateConverter}}" Width="SizeToCells" />
</DataGrid.Columns>
</DataGrid>
ADDITIONAL INFO:
This xaml DOES work and displays correctly, but I'd like the columns to autosize and so a DataGrid would be preferable:
<ListView ItemsSource="{Binding Charges}">
<ListView.View>
<GridView>
<GridViewColumn Header="Count" DisplayMemberBinding="{Binding Count, Mode=OneWay}" />
<GridViewColumn Header="Charge" DisplayMemberBinding="{Binding LawId, Mode=OneWay, Converter={StaticResource LawConverter}}" />
<GridViewColumn Header="Incident Date" DisplayMemberBinding="{Binding IncidentDate, Mode=OneWay, Converter={StaticResource DateConverter}}" />
<GridViewColumn Header="Convicted" DisplayMemberBinding="{Binding IsConvicted, Mode=OneWay}" />
<GridViewColumn Header="Disposition" DisplayMemberBinding="{Binding ChargeDispositionCodeId, Mode=OneWay, Converter={StaticResource CodeConverter}, ConverterParameter='ChargeDispositionCode'}" />
<GridViewColumn Header="Disposition Date" DisplayMemberBinding="{Binding DispositionDate, Mode=OneWay, Converter={StaticResource DateConverter}}" />
</GridView>
</ListView.View>
</ListView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
初始化网格后,检查输出调试器窗口。您收到任何绑定错误消息吗?
您还需要关闭“AutoGenerateColumns”,因为您是在 XAML 中显式定义列。
Check the Output debugger window when your grid is initialized. Do you get any Binding error messages?
You also need to turn off "AutoGenerateColumns" since you are explicitly defining the columns in your XAML.