调整父窗口大小时的 WPF 调整大小/对齐按钮

发布于 2024-12-23 11:52:19 字数 303 浏览 0 评论 0原文

我正在编写我的第一个 WPF 应用程序,当窗口最大化时,我在调整按钮大小/对齐按钮时遇到了一些问题。

本质上,我的应用程序顶部有一行大按钮。目前它们位于 Canvas 中,但我也尝试过 Grid 和 Stackpanel。每当我最大化(或者实际上只是扩展窗口)时,按钮基本上都会保持在同一个位置。我想要的是相对于其父容器重新定位按钮,或者拉伸按钮宽度以适应更改。

有人可以就实现这一目标的最佳容器和方法提供建议吗?我看过 Canvas.Top/Left、HorzontalAlignment、Margin 等,但我似乎找不到正确的组合。

谢谢。

I'm writing my first WPF app, and i'm having a few problems with resizing/aligning buttons when the window is maximised.

Essentially I have a single row of large buttons at the top of my application. Currently they are in a Canvas, but I have also tried Grid and Stackpanel. Whenever I maximise (or indeed just expand my window), the buttons essentially stay in the same place. What I want is to either reposition the buttons relative to its parent container, or stretch the button width to accommodate the change.

Can anybody offer advice on what is the best container and method to achieve this? I've looked at Canvas.Top/Left, HorzontalAlignment, Margin etc, but I can't seem to find the correct combination.

Thanks.

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

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

发布评论

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

评论(4

苏别ゝ 2024-12-30 11:52:19

WPF 中的做法是所有内容都应在 MainGrid 中对齐。
例如,如果将按钮 1 放入 Grid.Row 3 和 Grid.Column 2 中,它将保留在那里。

如果更改窗口大小,网格的大小也会调整,但控件仍保留在原来的位置。
您还可以使网格行/列固定宽度、自动调整大小或百分比。

与您的控件相同。所以知道了这一点,这只是一个玩弄它的问题。

这是一个例子。只需尝试一下宽度:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="43*" />
        <RowDefinition Height="222*" />
        <RowDefinition Height="35*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="113*" />
        <ColumnDefinition Width="168*" />
        <ColumnDefinition Width="119*" />
    </Grid.ColumnDefinitions>
    <Button Content="TEST" Grid.Column="2" Grid.Row="1" 
     HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" />
</Grid>

制作一些自动,制作一些固定,最后您就会明白目标。

The way to go in WPF is all content should be aligned in your MainGrid.
If you put Button 1 in for example Grid.Row 3 and Grid.Column 2, it will remain there.

If you change your window size, the grid gets resized, but the controls remain in their place.
You can also make your grid rows/columns fixed width, autosize, or percentage.

Same with your controls. So knowing this, it's just a matter of playing around with it.

Here is an example. Just play around with the Widths:

<Grid x:Name="LayoutRoot" Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="43*" />
        <RowDefinition Height="222*" />
        <RowDefinition Height="35*" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="113*" />
        <ColumnDefinition Width="168*" />
        <ColumnDefinition Width="119*" />
    </Grid.ColumnDefinitions>
    <Button Content="TEST" Grid.Column="2" Grid.Row="1" 
     HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" />
</Grid>

Make some Auto, make some fixed and at the end you'll understand the goal.

黯然 2024-12-30 11:52:19

为了您的目的,我将使用一个 DockPanel,其中一个面板停靠在顶部。或者,如果您希望按钮能够拉伸,则可以使用 UniformGrid。

<DockPanel>
  <StackPanel DockPanel.Dock="Top">
    <Button Content="Button1"/>
    <Button Content="Button2"/>
    <Button Content="Button3"/>
  </StackPanel
  <Grid>
    <!-- Stuff in the main part of the window here -->
  </Grid>
</DockPanel>

I would use a DockPanel with a Panel docked to the top for your purpose. Alternatively, you could use UniformGrid if you'd want the buttons to stretch.

<DockPanel>
  <StackPanel DockPanel.Dock="Top">
    <Button Content="Button1"/>
    <Button Content="Button2"/>
    <Button Content="Button3"/>
  </StackPanel
  <Grid>
    <!-- Stuff in the main part of the window here -->
  </Grid>
</DockPanel>
心奴独伤 2024-12-30 11:52:19

您可以使用 @De-ruige 提到的网格。

除了帮助您理解网格的含义之外,还可以在网格定义中使用属性“ShowGridLines”,如下所示:

<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">

另外,如果您想向网格中的几个单元格添加按钮等控件,您可以使用这些: Grid.RowSpan / Grid.ColumnSpan

Grid.RowSpan -
MSDN

You can use a grid as @De-ruige mentioned.

In addition to help you understand the meaning of a grid, use the property "ShowGridLines" in your grid definition, like that:

<Grid x:Name="LayoutRoot" Background="White" ShowGridLines="True">

Also, if you want to add a control like a button to few cells in your grid, you can use these: Grid.RowSpan / Grid.ColumnSpan.

Grid.RowSpan -
MSDN

是你 2024-12-30 11:52:19

Horizo​​ntalAlignment 更改为“Right”,将 VerticalAlignment 更改为“Bottom”。

嘭。问题解决。

Change HorizontalAlignment to "Right" and VerticalAlignment to "Bottom".

Bam. Problem Solve.

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