为 WPF Grid 指定 ScrollViewer 减去滚动条的宽度
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是在 ScrollViewer 容器大小和网格大小之间设置正确的依赖关系。让我们只考虑宽度:上面的绑定允许 ScrollViewer 仅在必要时公开水平滚动条:
放大网格的单个列(例如,使用拆分器)不会引起其他列的大小调整(如果出现这种情况,则会发生这种情况)您绑定的是
Width
而不是MinWidth
)。使用
ViewportWidth
而不是ActualWidth
会考虑滚动条宽度(如果有)。试试吧
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
insteadMinWidth
).Using the
ViewportWidth
instead ofActualWidth
takes into account the scrollbar width, if there is one.Just try
此处找到了解决方案。我给了我的顶部
Grid
一个Width
:Found the solution here. I gave my top
Grid
aWidth
: