简体   繁体   English

WPF 滚动自动调整大小面板

[英]WPF Scrolling AutoSizing Panel

I have a WPF application in which the main window holds a DockPanel with two children.我有一个 WPF 应用程序,其中主要的 window 拥有一个带有两个子项的 DockPanel。 The top child is another DockPanel which holds the menu and is of a fixed size.顶部子项是另一个 DockPanel,它包含菜单并且具有固定大小。 The lower child is the main work area, which should fill the remaining space and be resizable along with the window. (Hence the DockPanel parent.) Draggable objects get placed in this work area and might appear anywhere inside it.下方的子项是主要工作区,它应填充剩余空间并可与 window 一起调整大小。(因此是 DockPanel 父项。)可拖动对象放置在该工作区中,并可能出现在其中的任何位置。

I'm trying to figure out how to make scroll bars appear if an object is dragged outside the visible area. 我试图弄清楚如果将 object 拖到可见区域之外如何使滚动条出现。 The approximate XAML structure currently goes 当前的近似 XAML 结构
<Window> <DockPanel> <DockPanel with fixed-size content... > <ScrollViewer HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"> <Grid ClipToBounds="True" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"/> </ScrollViewer> </DockPanel> </Window>

So far I've tried a Grid and a Canvas. Both have built-in scroll bars, but they won't appear unless dimensions are specified--but if I apply dimensions, then the panel won't automatically resize to fill the work area.到目前为止,我已经尝试过 Grid 和 Canvas。两者都有内置滚动条,但除非指定尺寸,否则它们不会出现——但如果我应用尺寸,则面板不会自动调整大小以填充工作区域。 Then I tried surrounding the panel with a ScrollViewer.然后我尝试用 ScrollViewer 包围面板。 The unconstrained panel now successfully auto-resizes to fill the space, but the ScrollViewer has the same problem as the panel--it will only display scroll bars if it's constrained to hard dimensions.不受约束的面板现在可以成功地自动调整大小以填充空间,但是 ScrollViewer 与面板有同样的问题——如果它被限制为硬尺寸,它只会显示滚动条。

I'm thinking that this would work if I could dynamically constrain the ScrollViewer. 我在想,如果我可以动态约束 ScrollViewer,这会起作用。 So far, I haven't found any reliable way to dynamically apply size values to the ScrollViewer. 到目前为止,我还没有找到任何可靠的方法来动态地将大小值应用到 ScrollViewer。 Is there a way to create a Binding between the ScrollViewer dimensions and the ActualHeight and ActualWidth of the Grid? 有没有办法在 ScrollViewer 尺寸与网格的 ActualHeight 和 ActualWidth 之间创建绑定? Or, is there a way I can define the ActualHeight/ActualWidth of the grid as a DynamicResource that can be applied to the ScrollViewer? 或者,有没有一种方法可以将网格的 ActualHeight/ActualWidth 定义为可以应用于 ScrollViewer 的 DynamicResource? Or is there some other panel or method or resource that can be used so that all three criteria (panel fills available space, panel auto-resizes with window, anything dragged outside visible area triggers scroll bars) are met? 或者是否可以使用其他一些面板、方法或资源来满足所有三个条件(面板填充可用空间、面板自动调整大小为 window、任何拖到可见区域之外的内容都会触发滚动条)? Thanks in advance for any help. 在此先感谢您的帮助。

The problem was that I did not have a DockPanel.Dock setting on the bottom child of the containing DockPanel.问题是我在包含 DockPanel 的底部子项上没有 DockPanel.Dock 设置。 Relying on the DockPanel's LastChildFill wasn't enough to do the job.依靠 DockPanel 的 LastChildFill 不足以完成这项工作。 Once I set DockPanel.Dock = Bottom on the bottom child, the scroll bars started working.一旦我在底部的孩子上设置 DockPanel.Dock = Bottom ,滚动条就开始工作了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM