如何使用DockStyle.Fill在WPF标准控件? [英] How to use DockStyle.Fill for standard controls in WPF?

查看:3336
本文介绍了如何使用DockStyle.Fill在WPF标准控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很新的WPF。我是从窗户形式的使用,我创建了一个面板,里面的控制措施,并DockStyle.Fill给他们最大输出其大小与周围面板。

I'm very new to WPF. 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.

在WPF我想有相同的。我有一个标签控制,我想它的大小,以填补尽可能多的形式可能。
我有一个色带控制(RibbonControlsLibrary),并希望表单的其余部分将填充在最大尺寸的标签控件。

In WPF I want to have the same. I have a tab-control 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 tab control at max size.

(我不希望对接控制像停靠在Visual Studio中,只是老对接机制)

(I do not want to dock controls like docking in Visual Studio, just old docking mechanisms)

推荐答案

在WPF的WinForms相当于DockStyle.Fill的是:

The WPF equivalent of WinForms' DockStyle.Fill is:

HorizontalAlignment="Stretch" VerticalAlignment="Stretch"

这是几乎控件的默认,所以一般你不必做任何事情有一个WPF控件填写其父容器 :他们这样做自动。这对于那些不挤自己的孩子最小尺寸的所有容器真实的。

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.

常见误区

我现在解释说,防止几种常见的错误的Horizo​​ntalAlignment =拉伸VerticalAlignment =拉伸按预期工作。

I will now explain several common mistakes that prevent HorizontalAlignment="Stretch" VerticalAlignment="Stretch" from working as expected.

1。显式高度或宽度

一个常见的​​错误是明确指定宽度或高度的控制。所以,如果你有这样的:

One common mistake is to explicitly specify a Width or Height for a control. So if you have this:

<Grid>
  <Button Content="Why am I not filling the window?" Width="200" Height="20" />
  ...
</Grid>



只需卸下width和height属性:

Just remove the Width and Height attributes:

<Grid>
  <Button Content="Ahhh... problem solved" />
  ...
</Grid>



2。含面板​​挤压控制,以最小尺寸

另一个常见的​​错误是有包含面板挤压你的控制紧张,因为它会去。例如,一个垂直的StackPanel总是挤它的内容垂直小,因为他们会去:

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:

<StackPanel>
  <Button Content="Why am I squished flat?" />
</StackPanel>



切换到另一个面板,你会好到哪里去:

Change to another Panel and you'll be good to go:

<DockPanel>
  <Button Content="I am no longer squished." />
</DockPanel>



此外,任何网格行或列的高度为自动,同样会挤压其在该方向上的内容

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:


  • ContentControls千万不要挤他们的子女(包括边框,按钮,复选框,ScrollViewer中,和许多其他人)

  • 电网与单个行和列

  • 网带*号尺寸的行和列

  • DockPanel中不挤它的最后一个子

  • 的TabControl不挤它的内容

  • ContentControls never squeeze their children (this includes Border, Button, CheckBox, ScrollViewer, and many others)
  • Grid with a single row and column
  • Grid with "*" sized rows and columns
  • DockPanel doesn't squeeze its last child
  • TabControl doesn't squeeze its content

这做挤压子女容器的一些例子是:

Some examples of containers that do squeeze their children are:


  • 的StackPanel在其定位方向

  • 网格挤压与自动大小的行或列在该方向

  • DockPanel中挤出所有,但其最后的孩子在他们的码头方向
  • 的TabControl挤压其标题(所显示的选项卡上)

  • StackPanel squeezes in its Orientation direction
  • Grid with an "Auto" sized row or column squeezes in that direction
  • DockPanel squeezes all but its last child in their dock direction
  • TabControl squeezes its header (what is displayed on the tab)

3。显式高度或宽度进一步退出

这是惊人的多少次,我看到网格或DockPanel中给出一个明确的高度和宽度,像这样的:

It's amazing how many times I see Grid or DockPanel given an explicit height and width, like this:

<Grid Width="200" Height="100">
  <Button Content="I am unnecessarily constrainted by my containing panel" />
</Grid>

在一般你永远不希望给任何小组一个明确的高度或宽度。我的诊断布局问题时,第一个步骤是清除每一个明确的高度或宽度,我可以找到。

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。窗口是SizeToContent时,它不应该是

当您使用SizeToContent,您的内容会被挤压到最小尺寸。在许多应用中,这是非常有用的,是正确的选择。但是,如果你的内容没有天然的大小,那么你可能想省略SizeToContent。

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.

这篇关于如何使用DockStyle.Fill在WPF标准控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆