繁体   English   中英

意外的自动网格列宽

[英]Unexpected auto grid column width

下面的代码是我在Project中使用的wpf窗口的一小段。 它产生链接的wpf窗口。

我想知道为什么我的最后一个网格列这么宽。 我期望最后一列的宽度取决于按钮的宽度,因为该列的宽度设置为“自动”。 如果我删除StackPanel的columnpan,列的宽度将是正确的,但CheckBox不会与右侧对齐。

希望您了解我的问题。 我的目标是,最后一列越小越好,复选框在右侧,其余部分保持在该位置。

因为此代码段是更大的wpf窗口的一部分,所以我无法删除任何网格行或列。

非常感谢您的帮助。

最好的祝福。

WPF窗口

<Window x:Class="TestProject.Window1"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         d:DesignHeight="152.429" d:DesignWidth="403">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="Auto" />
    </Grid.ColumnDefinitions>

    <TextBox Grid.Column="0" 
             Grid.Row="0"
             Margin="5"
             Grid.ColumnSpan="2"/>

    <Button Grid.Column="2" 
            Grid.Row="0"
            Margin="5"
            Width="40"/>

    <ComboBox Grid.Column="0"
              Grid.Row="1"
              Margin="5"
              Grid.ColumnSpan="3"/>

    <Image Grid.Column="0"
           Grid.Row="2"/>

    <StackPanel Grid.Column="1"
                Grid.Row="2"
                Grid.ColumnSpan="2">

        <CheckBox Margin="5"
                  Content="checkbox content 1"/>

        <CheckBox Margin="5,0,5,5"
                  Content="checkbox content 2"/>
    </StackPanel>
</Grid>

您可以在另一个网格内放置一个网格。

这是将帮助您实现目标的代码。

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

        <TextBox Grid.Column="0" 
             Grid.Row="0"
             Margin="5"
             Grid.ColumnSpan="2"/>

        <Button Grid.Column="2" 
            Grid.Row="0"
            Margin="5"
            Width="40"/>

        <ComboBox Grid.Column="0"
              Grid.Row="1"
              Margin="5"
              Grid.ColumnSpan="3"/>

        <Grid Name="GridInsideAGrid"
                Grid.Column="0"
                Grid.Row="2"
                Grid.ColumnSpan="3">

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

            <Image Grid.Column="0" />
            <StackPanel Grid.Column="1">
                <CheckBox Margin="5"
                  Content="checkbox content 1"/>

                <CheckBox Margin="5,0,5,5"
                  Content="checkbox content 2"/>
            </StackPanel>
        </Grid>
    </Grid>

暂无
暂无

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

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