C# 在 WPF 中自动调整窗口内容大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2098463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 23:29:00  来源:igfitidea点击:

Auto resize window content in WPF

c#wpfautosize

提问by Mohit Deshpande

When debugging my WPF programs I have noticed that when the window is the set size, the contols look just fine. But when the window is maximized, the content is positioned at the same place as if window has not resized. I want it so that the content and window resize proportionately. How can I do this? Sorry if it is a noobish question, but I'm kinda new in the WPF era.

在调试我的 WPF 程序时,我注意到当窗口是设置的大小时,控件看起来很好。但是当窗口最大化时,内容被定位在同一个位置,就好像窗口没有调整大小一样。我希望它能够按比例调整内容和窗口的大小。我怎样才能做到这一点?对不起,如果这是一个菜鸟问题,但我在 WPF 时代有点新。



The XAML code is not completely ready yet but here is some of elements:

XAML 代码还没有完全准备好,但这里有一些元素:

<DockPanel>
    <StackPanel DockPanel.Dock="Left">
    ...
    </StackPanel>

    <TabControl DockPanel.Dock="Right">
    ...
    </TabControl>

    <ListView>
    ...
    </ListView>
</DockPanel>

采纳答案by David Veeneman

Usually, this is because dimension values are set statically, rather than dynamically. Here's the static approach:

通常,这是因为维度值是静态设置的,而不是动态设置的。这是静态方法:

<RowDefinition x:Name="NavigatorRow" Height="120"/>
<RowDefinition x:Name="TaskPanelRow" Height="80"/>

Both rows will have fixed heights, and they won't resize with the window.

两行都有固定的高度,它们不会随着窗口调整大小。

Here is the dynamic approach:

这是动态方法:

<RowDefinition x:Name="NavigatorRow" Height="1*"/> 
<RowDefinition x:Name="TaskPanelRow" Height="80"/>

The bottom row still has a fixed height of 80, but the top row will expand to fill whatever space is available. In other words, the rows will resize with the window. Columns work the same way.

底行仍然具有 80 的固定高度,但顶行将扩展以填充任何可用空间。换句话说,行将随着窗口调整大小。列的工作方式相同。

If I had three rows, I could do this:

如果我有三行,我可以这样做:

<RowDefinition x:Name="NavigatorRow" Height="1*"/>
<RowDefinition x:Name="CalendarRow" Height="2*"/>
<RowDefinition x:Name="TaskPanelRow" Height="80"/>

The Navigator Row and the Calendar Row will share the available space, with the Calendar Row taking twice the height of the Navigator Row. You get the idea.

导航器行和日历行将共享可用空间,日历行占据导航器行高度的两倍。你明白了。

So, it's not the container you use, but how you size that container. The one exception, as noted above, is the StackPanel, which does not scale. Use a Grid instead, since it does scale.

所以,重要的不是你使用的容器,而是你如何调整容器的大小。如上所述,一个例外是 StackPanel,它不能缩放。改用网格,因为它可以缩放。

回答by keithwarren7

Usually this is because the content is hosted in a container which has an explicitly set width and height - like Grid for example.

通常这是因为内容托管在一个容器中,该容器具有明确设置的宽度和高度 - 例如 Grid。

Post your Xaml or that answer is the best you will get!

发布您的 Xaml 或该答案是您将获得的最佳答案!

回答by Joey

Well, you have to have some sort of container for your controls, right? If you're using Canvas and just position your controls absolutely inside there you're pretty much out of luck; this isn't very well for scaling interfaces.

好吧,您必须为控件提供某种容器,对吗?如果您正在使用 Canvas 并且只是将您的控件绝对放置在那里,那么您就很不走运了;这对于缩放接口来说不是很好。

However, there are various container controls that will layout whatever you put in them in certain ways. And if used properly, they scale with a resizing window, too. The Grid is pretty flexible, but StackPanel and DockPanel are very handy at times, too.

但是,有各种容器控件可以以某种方式布置您放入其中的任何内容。如果使用得当,它们也会随着调整大小的窗口进行缩放。Grid 非常灵活,但 StackPanel 和 DockPanel 有时也非常方便。

You can nest them, if you need.

如果需要,您可以嵌套它们。

回答by Daniel

Avoid using StackPanels they don't resize dynamically properly.

避免使用它们不能正确动态调整大小的 StackPanel。

Ideally you should use a grid and specify percentages if you want things to resize proportionately.

理想情况下,如果您希望按比例调整大小,您应该使用网格并指定百分比。

回答by Tigran

Use WPF grid with the Widht and Height properties setupped with the "Number*" notion. For example Width="0.6*", wich is not absolute height but proportional relation to the container. Generaly , if you want resizable content, avoid the fixed size properties as much as you can.

将 WPF 网格与使用“数字*”概念设置的宽度和高度属性一起使用。例如Width="0.6*",它不是绝对高度,而是与容器的比例关系。一般来说,如果您想要可调整大小的内容,请尽可能避免使用固定大小的属性。

  <Grid.RowDefinitions>
       <RowDefinition Height="10*"></RowDefinition>                       
       <RowDefinition Height="*" ></RowDefinition>
  </Grid.RowDefinitions>

Good Luck.

祝你好运。

回答by Brady Moritz

Not sure why everyone is saying stackpanels don't resize dynamically. They handle resizing just like grids do. Just make sure you set your HorizontalAlignment="Stretch" (and/or VerticalAlignment) and the content will expand to fill the stackpanel size. I'm currently using a UI that consists of many nested stackpanels, horizontal and vertical resizing the window expands/contracts all the controls equally inside the window.

不知道为什么每个人都说堆栈面板不会动态调整大小。他们像网格一样处理调整大小。只需确保设置 Horizo​​ntalAlignment="Stretch"(和/或 VerticalAlignment),内容将扩展以填充堆栈面板大小。我目前使用的 UI 由许多嵌套的堆栈面板组成,水平和垂直调整窗口大小会在窗口内同等地扩展/收缩所有控件。