简体   繁体   English

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

[英]WPF Resizing/Aligning Buttons when parent window resized

I'm writing my first WPF app, and i'm having a few problems with resizing/aligning buttons when the window is maximised. 我正在编写我的第一个WPF应用程序,当窗口最大化时,我在调整按钮大小/对齐按钮方面遇到了一些问题。

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. 目前他们在Canvas中,但我也尝试过Grid和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. 我看过Canvas.Top/Left,HorzontalAlignment,Margin等,但我似乎无法找到正确的组合。

Thanks. 谢谢。

The way to go in WPF is all content should be aligned in your MainGrid. WPF的方法是在MainGrid中对齐所有内容。 If you put Button 1 in for example Grid.Row 3 and Grid.Column 2, it will remain there. 如果你把Button 1放在例如Grid.Row 3和Grid.Column 2中,它将保留在那里。

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. 做一些自动,做一些修复,最后你会理解目标。

I would use a DockPanel with a Panel docked to the top for your purpose. 我会使用一个DockPanel,并将面板停靠在顶部以达到您的目的。 Alternatively, you could use UniformGrid if you'd want the buttons to stretch. 或者,如果您想要拉伸按钮,可以使用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>

You can use a grid as @De-ruige mentioned. 您可以使用@ De-ruige提到的网格。

In addition to help you understand the meaning of a grid, use the property "ShowGridLines" in your grid definition, like that: 除了帮助您理解网格的含义之外,还可以在网格定义中使用属性“ShowGridLines”,如下所示:

<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.ColumnSpanGrid.RowSpan / Grid.ColumnSpan

Grid.RowSpan - MSDN Grid.RowSpan - MSDN

Change HorizontalAlignment to "Right" and VerticalAlignment to "Bottom". HorizontalAlignment更改为“Right”,将VerticalAlignment更改为“Bottom”。

Bam. 巴姆。 Problem Solve. 问题解决。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM