在wpf中如何使数据网格适合窗口高度

发布于 2024-11-19 17:14:03 字数 729 浏览 4 评论 0原文

我有一个包含 3 列和 2 行的网格,

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

位于左下单元格,我有一个数据网格,其中 AutoGenerateColumns=True 可以加载多行。我想要做的是使数据网格高度最大化以适合窗口,并且用户能够使用数据网格滚动条上下滚动行。

发生的情况是数据网格从窗口底部流动,即使我设置了

ScrollViewer.VerticalScrollBarVisibility="Visible"

数据网格,滚动条也没有效果,行向下流动。不知怎的,数据网格并没有感觉到受到限制......

该怎么办?

I have a grid with 3 columns and 2 rows

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="10"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

I the lower left cell, i have a data grid, with AutoGenerateColumns=True which can load many rows. What I want to do is for the data grid height to maximize to fit the window, and for the user to be able to use the datagrid scrollbar to scroll the rows up and down.

What happens is that the datagrid flows of the bottom of the window, and even if i set the

ScrollViewer.VerticalScrollBarVisibility="Visible"

of the datagrid, the scrollbar has no effect and the rows flow downwards. Somehow the datagrid does not feel restricted...

What to do?

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

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

发布评论

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

评论(3

倾城花音 2024-11-26 17:14:03

尝试设置 DataGrid 的 Horizo​​ntalAlignment=StretchVerticalScrollBarVisibility=Auto

如果这不起作用,您可能还需要将网格的高度绑定到窗口高度,这样它就不会起作用t 自动增长以适应其内容。通常我使用 Height="{BindingrelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}" (可能是 RenderSize.ActualHeight 而不是只是 ActualHeight...我忘记了

另一种选择是使用 DockPanel 而不是 Grid 因为该控件没有。相反,它会拉伸最后一个子元素以填充剩余空间。

Try setting your DataGrid's HorizontalAlignment=Stretch and VerticalScrollBarVisibility=Auto

If that doesn't work, you may also need to bind the Grid's Height to the Window Height so that it doesn't auto-grow to fit its contents. Usually I use Height="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=ActualHeight}" (It might be RenderSize.ActualHeight instead of just ActualHeight... I forgot.

Another alternative is to use a DockPanel instead of a Grid since that control doesn't auto-grow to fit its contents. Instead it'll stretch its last child to fill the remaining space.

还如梦归 2024-11-26 17:14:03

我遇到了同样的问题,但绑定到窗口高度并没有完全解决我的问题。在我的例子中,DataGrid 仍然延伸到窗口可视区域下方 2 到 3 英寸。我相信这是因为我的 DataGrid 开始于窗口顶部下方约 2 到 3 英寸处。

最后我发现根本没有必要绑定DataGrid的高度。我所要做的就是更改 DataGrid 的直接容器。

对于我来说,以下 XAML 设置会导致在添加足够的行时 DataGrid 超出窗口的大小。请注意,DataGrid 位于 StackPanel 内。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
       <!-- StackPanel Content accounting for about 2-3 inches of space -->
    </StackPanel>
    <!-- DataGrid within a StackPanel extends past the vertical space of the Window
     and does not display vertical scroll bars.  Even if I bind the height to Window 
     height the DataGrid content still extends 2-3 inches past the viewable Window area-->
    <StackPanel Grid.Row="1">
    <DataGrid ItemsSource="{StaticResource ImportedTransactionList}" 
         Margin="10,20,10,10" MinHeight="100">
    </DataGrid>
    </StackPanel>
</Grid>

然而,简单地删除 StackPanel 就解决了我的问题。

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
       <!-- StackPanel Content accounting for about 2-3 inches of space -->
    </StackPanel>
    <!-- Removing the StackPanel fixes the issue-->
    <DataGrid Grid.Row="1" ItemsSource="{StaticResource SomeStaticResource}" 
           Margin="10,20,10,10" MinHeight="100">
    </DataGrid>
</Grid>

由于原始帖子相当旧,我应该注意到我正在使用 VS2017 和 .Net Framework 4.6.1,但我不确定这是否有任何影响。

I had the same problem but binding to the Window height did not completely solve the problem for me. In my case the DataGrid still extended 2 to 3 inches below the Window's viewable area. I believe this was because my DataGrid started about 2 to 3 inches below the top of the Window.

In the end I found that it was not necessary to bind the DataGrid's height at all. All I had to do was change the DataGrid's direct container.

For me, the following XAML setup causes the DataGrid to extend beyond the size of the Window when enough rows are added. Notice that the DataGrid sits within a StackPanel.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
       <!-- StackPanel Content accounting for about 2-3 inches of space -->
    </StackPanel>
    <!-- DataGrid within a StackPanel extends past the vertical space of the Window
     and does not display vertical scroll bars.  Even if I bind the height to Window 
     height the DataGrid content still extends 2-3 inches past the viewable Window area-->
    <StackPanel Grid.Row="1">
    <DataGrid ItemsSource="{StaticResource ImportedTransactionList}" 
         Margin="10,20,10,10" MinHeight="100">
    </DataGrid>
    </StackPanel>
</Grid>

However, simply removing the StackPanel fixed the problem for me.

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="75"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <StackPanel Grid.Row="0">
       <!-- StackPanel Content accounting for about 2-3 inches of space -->
    </StackPanel>
    <!-- Removing the StackPanel fixes the issue-->
    <DataGrid Grid.Row="1" ItemsSource="{StaticResource SomeStaticResource}" 
           Margin="10,20,10,10" MinHeight="100">
    </DataGrid>
</Grid>

As the original post is quite old, I should note that I am using VS2017 and .Net Framework 4.6.1 but I am not sure if this has any bearing.

我三岁 2024-11-26 17:14:03

您必须使用 * 定义 DataGrid 所在的列和行。
你说它在左下角的单元格上。该行没问题,但您的列有 Width="Auto"

如果 Width="Auto"AutoGenerateColumns=True 会造成混乱。

You have to define the column and row where the DataGrid is with *.
You say it is on the lower left cell. The row is ok but your column there has Width="Auto".

The AutoGenerateColumns=True gives a mess if Width="Auto".

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