【问题标题】:WPF. Auto columns not sizing the right way with ColumnSpanWPF。使用 ColumnSpan 自动调整列的大小不正确
【发布时间】:2017-01-24 09:07:14
【问题描述】:

我遇到了自动列的大小未按我想要的方式调整的问题。 我正在使用以下代码:

<Grid>
    <Grid.ColumnDefinitions>
         <ColumnDefinition Width="auto"/>
         <ColumnDefinition Width="auto"/>
    </Grid.ColumnDefinitions>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto"/>
        <RowDefinition Height="10"/>
        <RowDefinition Height="auto"/>
    </Grid.RowDefinitions>
    <Grid Grid.ColumnSpan="2">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"/>
            <ColumnDefinition Width="5"/>
            <ColumnDefinition Width="300"/>
        </Grid.ColumnDefinitions>
        <TextBlock Text="User: "/>
        <TextBox Grid.Row="2" Grid.Column="2" />
    </Grid>
    <Button Grid.Row="2" Grid.Column="1" Content="Search"/>
</Grid>

我希望它工作的方式是第二个自动列采用按钮所需的宽度(使其在网格正下方对齐),而第一个自动列采用其余部分所需的剩余宽度网格。

但是,第二列变得更长,并且宽度被划分为 * 列。看下面的截图

link to image as I do not have the right rep to post

这是我对自动列的工作方式的误解吗?如果是这样,最好的解决方法是什么。请注意,这是简化的代码,因为它是包含多于一列的表单的一部分,因此我宁愿避免将我的对象包装在其他网格或堆栈面板中。

在按钮上设置宽度也是不可能的,因为我希望它恰好是“搜索”内容所需的大小

编辑: 我想要实现的布局类似于this

【问题讨论】:

    标签: wpf button grid


    【解决方案1】:

    首先,这可以通过一个网格来实现。不需要子网格。您可以使用此代码。为了更好地理解,您能否提供您想要实现的图像。

      <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="auto"/>
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="auto"/>
                    <RowDefinition Height="10"/>
                    <RowDefinition Height="auto"/>
                </Grid.RowDefinitions>
                <TextBlock  Text="User: "/>
                <TextBox  Grid.Column="1" />
                <Button Grid.Row="2" Grid.Column="1" Content="Search" HorizontalAlignment="Right" Width="Auto"/>
            </Grid>
    

    【讨论】:

    • 我已经用我想要的截图编辑了我的问题。
    • 这对我有用,谢谢!仍然觉得我的代码也应该这样,但这是一个很好的解决方案
    • 谢谢@Benjamin Van den Broeck
    猜你喜欢
    • 2015-10-10
    • 1970-01-01
    • 2012-07-03
    • 2011-05-21
    • 1970-01-01
    • 1970-01-01
    • 2010-12-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多