为 WPF Grid 指定 ScrollViewer 减去滚动条的宽度

发布于 2024-10-14 14:40:49 字数 1187 浏览 7 评论 0原文

我有一个 WPF UserControl,其中两个网格彼此重叠。底部网格位于 ScrollViewer 中。这个想法是让第一个网格成为第二个网格的标题。然而,我在列的宽度方面遇到了麻烦。两个网格都应该占据尽可能多的空间(窗口的宽度),但是顶部网格当然应该稍微宽一点,因为底部网格的右侧有一个滚动条。

这是我得到的简化的 Xaml:

<UserControl>
    <DockPanel>
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        </Grid>
        <ScrollViewer>
            <Grid>
                <Grid.ColumnDefintions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefintions>
            </Grid>
        </ScrollViewer>
    </DockPanel>
</UserControl>

实际上渲染得很好,除了顶部网格的最右边的列延伸到滚动条上,这是我想避免的。

这是结果的图像:网格列和滚动条的宽度。红色表示列/单元格现在所在的位置,但我希望它停在蓝线处。我尝试过 SharedSizeGroups,但这似乎使我的网格再次变小(不占用窗口的全部空间)。

I have a WPF UserControl with two Grids above each other. The bottom Grid is in a ScrollViewer. The idea is to have the first Grid be the header of the second Grid. I'm having trouble with the width of the columns however. Both Grids should take up all the space they can (the width of the Window), but the top Grid should of course be a little less wide, because there's a scrollbar on the right of the bottom Grid.

This is the simplified Xaml I've got:

<UserControl>
    <DockPanel>
        <Grid DockPanel.Dock="Top">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
        </Grid>
        <ScrollViewer>
            <Grid>
                <Grid.ColumnDefintions>
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="*" />
                </Grid.ColumnDefintions>
            </Grid>
        </ScrollViewer>
    </DockPanel>
</UserControl>

This renders fine actually, except that the most right column of the top Grid extends over the scrollbar, which I want to avoid.!

Here is an image of the result: Grid column and width of scrollbar. The red indicates where the column/cell is now, but I want it to stop at the blue line. I've tried SharedSizeGroups, but that seems to make my Grids small again (not take up the full space of the window).

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

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

发布评论

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

评论(2

樱花落人离去 2024-10-21 14:40:49
<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Name="sv">
    <Grid MinWidth="{Binding ElementName=sv, Path=ViewportWidth}" MinHeight="{Binding ElementName=sv, Path=ViewportHeight}" />
</ScrollViewer>

问题是在 ScrollViewer 容器大小和网格大小之间设置正确的依赖关系。让我们只考虑宽度:上面的绑定允许 ScrollViewer 仅在必要时公开水平滚动条:

放大网格的单个列(例如,使用拆分器)不会引起其他列的大小调整(如果出现这种情况,则会发生这种情况)您绑定的是 Width 而不是 MinWidth)。

使用 ViewportWidth 而不是 ActualWidth 会考虑滚动条宽度(如果有)。

试试吧

<ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto" Name="sv">
    <Grid MinWidth="{Binding ElementName=sv, Path=ViewportWidth}" MinHeight="{Binding ElementName=sv, Path=ViewportHeight}" />
</ScrollViewer>

The problem is setting up a correct dependency between the ScrollViewer container size and the grid size. Let us consider just the width: the binding above allows the ScrollViewer to expose the horizontal scrollbar only if necessary:

enlarging a single column of the grid (eg. with a splitter) does not provoke the resizing of the other columns (which would happen if you bind Width instead MinWidth).

Using the ViewportWidth instead of ActualWidth takes into account the scrollbar width, if there is one.

Just try

℡寂寞咖啡 2024-10-21 14:40:49

此处找到了解决方案。我给了我的顶部 Grid 一个 Width

Width="{Binding ElementName=BottomGrid, Path=ActualWidth}"

Found the solution here. I gave my top Grid a Width:

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