如何在WPF中使用DockStyle.Fill作为标准控件?
我使用 Windows 窗体,创建一个面板,将控件放置在其中,并为它们提供 DockStyle.Fill
以最大程度地扩大其周围面板的大小。
在 WPF 中我想要同样的。我有一个 TabControl,我希望它的大小尽可能多地填充表单。 我有一个功能区控件(RibbonControlsLibrary),并希望表单的其余部分以最大尺寸填充 TabControl。
(我不想像 Visual Studio 中的停靠那样停靠控件,只是旧的停靠机制)
I'm used from windows forms, that I create a panel, place controls inside it and give them DockStyle.Fill
to max out their size to the surrounding panel.
In WPF I want to have the same. I have a TabControl and I want its size to fill as much of the form as possible.
I have a ribbon control (RibbonControlsLibrary) and want the rest of the form to be filled with the TabControl at max size.
(I do not want to dock controls like docking in Visual Studio, just old docking mechanisms)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
WinForms 的 DockStyle.Fill 的 WPF 等效项是:
这是几乎所有控件的默认设置,因此一般情况下您无需执行任何操作即可让 WPF 控件填充其父容器:自动执行此操作。对于所有不将其子级挤压到最小尺寸的容器来说都是如此。
常见错误
现在,我将解释几个导致
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
无法按预期工作的常见错误。1.显式高度或宽度
一个常见的错误是显式指定控件的宽度或高度。因此,如果您有这样的情况:
只需删除宽度和高度属性:
2。包含面板将控件挤压到最小尺寸
另一个常见的错误是让包含面板将控件挤压得尽可能紧。例如,垂直 StackPanel 将始终将其内容垂直压缩到尽可能小:
更改为另一个面板,您就可以开始了:
此外,任何高度为“自动”的 Grid 行或列也会类似地压缩其内容那个方向。
不挤压其子级的容器的一些示例包括:
挤压其子级的容器的一些示例包括:
3。更进一步的显式高度或宽度
令人惊讶的是,我多次看到 Grid 或 DockPanel 给出了显式高度和宽度,如下所示:
一般来说,您永远不想为任何面板提供显式高度或宽度。诊断布局问题时,我的第一步是删除我能找到的所有显式高度或宽度。
4.窗口在不应该的情况下使用了 SizeToContent
当您使用 SizeToContent 时,您的内容将被压缩到最小尺寸。在许多应用中这非常有用并且是正确的选择。但如果您的内容没有“自然”大小,那么您可能需要省略 SizeToContent。
The WPF equivalent of WinForms' DockStyle.Fill is:
This is the default for almost controls, so in general you don't have to do anything at all to have a WPF control fill its parent container: They do so automatically. This is true for all containers that don't squeeze their children to minimum size.
Common Mistakes
I will now explain several common mistakes that prevent
HorizontalAlignment="Stretch" VerticalAlignment="Stretch"
from working as expected.1. Explicit Height or Width
One common mistake is to explicitly specify a Width or Height for a control. So if you have this:
Just remove the Width and Height attributes:
2. Containing panel squeezes control to minimum size
Another common mistake is to have the containing panel squeezing your control as tight as it will go. For example a vertical StackPanel will always squeeze its contents vertically as small as they will go:
Change to another Panel and you'll be good to go:
Also, any Grid row or column whose height is "Auto" will similarly squeeze its content in that direction.
Some examples of containers that don't squeeze their children are:
Some examples of containers that do squeeze their children are:
3. Explicit Height or Width further out
It's amazing how many times I see Grid or DockPanel given an explicit height and width, like this:
In general you never want to give any Panel an explicit Height or Width. My first step when diagnosing layout problems is to remove every explicit Height or Width I can find.
4. Window is SizeToContent when it shouldn't be
When you use SizeToContent, your content will be squeezed to minimum size. In many applications this is very useful and is the correct choice. But if your content has no "natural" size then you'll probably want to omit SizeToContent.
您只需说
DockPanel
LastChildFill="True"
即可完成您想要的操作,然后确保您想要填充的内容实际上是最后一个子项!Grid 是布局中的野兽,您可以用它来做大多数事情,但 DockPanel 通常是最外层布局面板的正确选择。这是一个伪代码示例:
You can do what you want by just saying
DockPanel
LastChildFill="True"
and then making sure what you want to be the filler is actually the last child!The Grid is the beast of a layout that you can make do most anything, but the DockPanel is usually the right choice for your outermost layout panel. Here is an psuedocode example:
只需将控件包装在两行网格中即可。网格将自动使用为其提供的所有空间,您可以通过给行高度“*”来定义行以占用所有剩余空间。我的示例中的第一行(高度=“自动”)将占用功能区所需的空间。希望有帮助。
通过将“Grid.Row=..”属性添加到网格的子控件,它们将被分配给网格的行。然后网格将根据行定义来调整其子项的大小。
just wrap your controls in a grid with two rows. The grid will automatically use all space given to it and you can define the rows to take up all space left by giving them a height of "*". The first row in my example (Height="Auto") will take as much space as needed by the ribbon. Hope that helps.
By adding the "Grid.Row=.." attribute to child controls of the grid they get assigned to rows of the grid. Then the grid will size it's children as defined by the row definitions.
我在这里尝试了很多方法但无法解决我的问题。 (在控件中填写其他控件)
直到我发现 it
示例:
为了方便设计
I was try many method here but can't solve my problem. (Fill other control in control)
Until i found it
Example:
For easy design