如何在WPF中将按钮位置设置为相对于网格单元边框的位置

huangapple go评论75阅读模式
英文:

How to set button position proportional to the border of grid cell border in WPF

问题

我正在重新排列WPF窗口中的元素。

我已将页面划分为3x3的网格,并希望将按钮放置在网格单元格中,并根据与网格单元格边框的距离比例来控制其大小。在这种情况下,当我调整整个窗口的大小时,按钮将相应地调整大小。

我想要的是按钮左侧到网格单元格左边框的距离是网格单元格宽度的20%,右侧到右边框的距离也是如此。顶部和底部应具有网格单元格高度的10%的距离。

示例XAML如下:

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

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

    <Button Content="BACK" Grid.Column="0" Grid.Row="2"/>
</Grid>

在XAML中是否有简单的方法来实现这一点,还是必须通过编程来完成?

英文:

I am rearranging the elements in a window in WPF.

I have divided the page into a 3*3 grid and I want to put a button in one of the grid cell and control its size depending on the distance proportion to the grid cell border. In that case when I resize the whole window, the button will resize accordingly.

What I want is the distance of left side of the button to the left border of the grid cell border is 20% of the width of the grid cell, so is the right side to the right border. The top side and bottom side should have the distance of 10% of the grid cell height.

The example xaml is:

&lt;Grid&gt;
    &lt;Grid.ColumnDefinitions&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
    &lt;/Grid.ColumnDefinitions&gt;

    &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
    &lt;/Grid.RowDefinitions&gt;

    &lt;Button Content=&quot;BACK&quot; Grid.Column=&quot;0&quot; Grid.Row=&quot;2&quot;/&gt;
&lt;/Grid&gt;

Is there an easy way to do that in XAML or it must be done programmatically?

答案1

得分: 1

使用另一个嵌套的3x3网格,并设置适当的行高和列宽。

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Column="0" Grid.Row="2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="3*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"/>
            <RowDefinition Height="8*"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <Button Grid.Column="1" Grid.Row="1" Content="BACK"/>
    </Grid>
</Grid>
英文:

Use another nested 3x3 Grid with appropriate row heights and column widths.

&lt;Grid&gt;
    &lt;Grid.ColumnDefinitions&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
        &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
    &lt;/Grid.ColumnDefinitions&gt;
    &lt;Grid.RowDefinitions&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;RowDefinition Height=&quot;*&quot;/&gt;
    &lt;/Grid.RowDefinitions&gt;

    &lt;Grid Grid.Column=&quot;0&quot; Grid.Row=&quot;2&quot;&gt;
        &lt;Grid.ColumnDefinitions&gt;
            &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
            &lt;ColumnDefinition Width=&quot;3*&quot;/&gt;
            &lt;ColumnDefinition Width=&quot;*&quot;/&gt;
        &lt;/Grid.ColumnDefinitions&gt;
        &lt;Grid.RowDefinitions&gt;
            &lt;RowDefinition Height=&quot;*&quot;/&gt;
            &lt;RowDefinition Height=&quot;8*&quot;/&gt;
            &lt;RowDefinition Height=&quot;*&quot;/&gt;
        &lt;/Grid.RowDefinitions&gt;

        &lt;Button Grid.Column=&quot;1&quot; Grid.Row=&quot;1&quot; Content=&quot;BACK&quot;/&gt;
    &lt;/Grid&gt;
&lt;/Grid&gt;

huangapple
  • 本文由 发表于 2023年7月17日 22:05:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/76705250.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定