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

发布于 2024-08-18 02:44:18 字数 439 浏览 7 评论 0原文

在调试 WPF 程序时,我注意到当窗口达到设定大小时,控件看起来很好。但是当窗口最大化时,内容位于同一位置,就好像窗口没有调整大小一样。我希望内容和窗口能够按比例调整大小。我该怎么做?抱歉,如果这是一个新手问题,但我对 WPF 时代还算陌生。


XAML 代码尚未完全准备好,但以下是一些元素:

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

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

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

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.


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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

浮萍、无处依 2024-08-25 02:44:18

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

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

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

下面是动态方法:

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

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

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

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

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

因此,重要的不是您使用的容器,而是您如何调整该容器的大小。如上所述,唯一的例外是 StackPanel,它无法缩放。请改用网格,因为它确实可以缩放。

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.

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.

噩梦成真你也成魔 2024-08-25 02:44:18

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

发布您的 Xaml,否则答案就是您将得到的最好答案!

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

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

烟若柳尘 2024-08-25 02:44:18

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

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

Avoid using StackPanels they don't resize dynamically properly.

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

始终不够爱げ你 2024-08-25 02:44:18

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

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.

鲸落 2024-08-25 02:44:18

好吧,你必须有某种容器来容纳你的控件,对吧?如果您使用 Canvas 并且只是将控件绝对放置在其中,那么您就很不走运了;这对于扩展接口来说不是很好。

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

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

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.

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.

You can nest them, if you need.

烟雨凡馨 2024-08-25 02:44:18

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

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

祝你好运。

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.

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

Good Luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文