WPF 数据网格 - 如何隐藏可见的下一列/行?

发布于 2024-09-25 07:30:44 字数 3197 浏览 3 评论 0原文

我正在使用 WPF 数据网格绑定到对象(即不是数据库)。我在拉伸上进行了水平对齐。我目前将它放在一个边框内,该边框位于一个网格内。

但是,当我运行该应用程序时,DataGrid 显示空白列和网格。也就是说我有 5 列 & 5 行,然后显示空白的第 6 列和行。请参阅图片:

alt text

问题 - 如何删除这些空白行?请注意,DataGrid 的内容是以编程方式填充的(即不是静态的)。

谢谢

编辑:我修复了额外的行(通过 CanUserAddRows),但我仍然对右侧的额外列有问题。

右侧额外列的问题似乎与自动设置列宽有关。 DataGrid 实际上已设置(请参阅下面的 XAML),以便在其右侧有一个 GridSplitter。当我移动 GridSplitter 时,我注意到 DataGrid 列不会自动调整大小。因此,总体而言,问题在于(a)启动时有部分可见的额外列,以及(b)移动 GridSplitter 后它们也不会调整大小。

关于如何让它发挥作用有什么想法吗?

        <Grid>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition  Height="Auto"/>
                    <RowDefinition  Height="Auto"/>
                    <RowDefinition  Height="*"/>
                </Grid.RowDefinitions>
                <Label Content="Summary" Grid.Row="0" HorizontalAlignment="Center" />
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=AllTime}" Grid.Column="0">All Time</RadioButton>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Month}" Grid.Column="1">Month</RadioButton>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Week}" Grid.Column="2">Week</RadioButton>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Day}" Grid.Column="3">Day</RadioButton>
                </Grid>
                <Border Grid.Row="2">
                    <DataGrid Name="SummaryDataGrid"  HorizontalGridLinesBrush="#FF726868" VerticalGridLinesBrush="#FF726868" AlternatingRowBackground="#FFD0F896" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserAddRows="False" CanUserSortColumns="True" CanUserResizeColumns="False" ColumnWidth="Auto" />
                </Border>

            </Grid>


            <GridSplitter HorizontalAlignment="Right" 
            VerticalAlignment="Stretch" Grid.Column="1" ResizeBehavior="PreviousAndNext"
            Width="5" Background="#FFBCBCBC"/>

            <Grid Grid.Column="2" Name="RTChartGrid">
                <-- CUT -->

            </Grid>


        </Grid>

I'm using a WPF data grid to bind to an object (i.e. not a database). I have the horizontal alignment on Stretch. I currently have it within a Border, which is within a Grid.

However when I run the application the DataGrid has a blank column and grid showing. That is say I have 5 columns & 5 rows, then there is a blank 6th column and row showing. See image:

alt text

Question - How can I get rid of these blank rows? Note that the contents of the DataGrid are populated programmatically (i.e. are not static).

thanks

EDIT: I got the extra row fixed (via CanUserAddRows), but I still have an issue with the extra column on the right.

The issue with the extra column on the right seems to be to do with automatically setting column widths. The DataGrid is actually setup (see XAML below) such that there is a GridSplitter just on it's right. When I move the GridSplitter I note that the DataGrid columns don't resize automatically. So overall the issue is both (a) on startup there is a partial extra column visible, and (b) after moving the GridSplitter they don't resize either.

Any ideas on how to get this working?

        <Grid>

            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0">
                <Grid.RowDefinitions>
                    <RowDefinition  Height="Auto"/>
                    <RowDefinition  Height="Auto"/>
                    <RowDefinition  Height="*"/>
                </Grid.RowDefinitions>
                <Label Content="Summary" Grid.Row="0" HorizontalAlignment="Center" />
                <Grid Grid.Row="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=AllTime}" Grid.Column="0">All Time</RadioButton>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Month}" Grid.Column="1">Month</RadioButton>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Week}" Grid.Column="2">Week</RadioButton>
                    <RadioButton IsChecked="{Binding Path=Period, Converter={StaticResource enumBooleanConverter}, ConverterParameter=Day}" Grid.Column="3">Day</RadioButton>
                </Grid>
                <Border Grid.Row="2">
                    <DataGrid Name="SummaryDataGrid"  HorizontalGridLinesBrush="#FF726868" VerticalGridLinesBrush="#FF726868" AlternatingRowBackground="#FFD0F896" CanUserReorderColumns="False" CanUserResizeRows="False" CanUserAddRows="False" CanUserSortColumns="True" CanUserResizeColumns="False" ColumnWidth="Auto" />
                </Border>

            </Grid>


            <GridSplitter HorizontalAlignment="Right" 
            VerticalAlignment="Stretch" Grid.Column="1" ResizeBehavior="PreviousAndNext"
            Width="5" Background="#FFBCBCBC"/>

            <Grid Grid.Column="2" Name="RTChartGrid">
                <-- CUT -->

            </Grid>


        </Grid>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

幽梦紫曦~ 2024-10-02 07:30:44

我只是将 Loaded 处理程序附加到 Datagrid,并将所有附加列的宽度设置为 0 并将其隐藏。

I simply attached a Loaded handler to the Datagrid and set all of the additional columns to a width of 0 and made them hidden.

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