DataGrid 不显示,ListView 工作正常吗?

发布于 2024-12-04 13:08:35 字数 2480 浏览 0 评论 0原文

认为我在这里缺少一些非常简单的东西,但我似乎无法让我的数据网格正确显示?

我最初使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初心未许 2024-12-11 13:08:35

初始化网格后,检查输出调试器窗口。您收到任何绑定错误消息吗?

您还需要关闭“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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文