wpf 如何使文本框扩展以填充网格单元格的剩余空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25268910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 11:20:09  来源:igfitidea点击:

How do i make the textboxes expand to fill the remaining space of the Grid Cell?

wpfxaml

提问by Phill Greggan

I have the following window with some input textboxes. But these textboxes will not expand to fill the remaining space of the second column. Furthermore when the window resizes the textboxesdoesn't resizeaccordingly,

我有以下带有一些输入的窗口textboxes。但是这些文本框不会扩展以填充第二列的剩余空间。此外,当窗口调整大小时,textboxes不会相应地调整大小

Here is my window

这是我的窗户

enter image description here

在此处输入图片说明

Here is my XAML markup

这是我的 XAML 标记

<Window x:Class="WpfApplication8.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Grid ShowGridLines="True">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="100"></ColumnDefinition>
            <ColumnDefinition Width="*"></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
            <RowDefinition Height="*"></RowDefinition>
            <RowDefinition Height="28"></RowDefinition>
        </Grid.RowDefinitions>

        <Label Content="First Name" Grid.Column="0" Grid.Row="0"></Label>
        <Label Content="Last Name" Grid.Column="0" Grid.Row="1"></Label>
        <Label Content="Street Name" Grid.Column="0" Grid.Row="2"></Label>
        <Label Content="Suburb" Grid.Column="0" Grid.Row="3"></Label>
        <Label Content="City" Grid.Column="0" Grid.Row="4"></Label>

        <TextBox Width="313" Grid.Column="1" Margin="3" HorizontalAlignment="Left"/>
        <TextBox Width="313" Grid.Column="1" Grid.Row="1" Margin="3" 
                 HorizontalAlignment="Left" ></TextBox>
        <TextBox Width="313" Grid.Column="1" Grid.Row="2" Margin="3" 
                 HorizontalAlignment="Left"></TextBox>
        <TextBox Width="313" Grid.Column="1" Grid.Row="3" Margin="3" 
                 HorizontalAlignment="Left"></TextBox>
        <TextBox Width="313" Grid.Column="1" Grid.Row="4" Margin="3"
                 HorizontalAlignment="Left"></TextBox>

        <StackPanel Orientation="Horizontal" Grid.Column="1" Grid.Row="5" 
                    HorizontalAlignment="Right">
        <Button Content="Save" Grid.Column="1" Grid.Row="5" Width="100" Margin="3" />
        <Button Content="Exit" Grid.Column="1" Grid.Row="5" Width="100" 
                 HorizontalAlignment="Right" Margin="3"></Button>
        </StackPanel>
        <!--<TextBox Width="313" Grid.Column="1"></TextBox>-->
    </Grid>
</Window>
  1. Is there away to expand the textboxesto fill the remaining space in the second column?
  2. Is there away to make the textboxesresize with the form resize?
  1. 是否可以扩展textboxes以填充第二列中的剩余空间?
  2. 是否textboxes可以通过表单调整大小来调整大小?

回答by d.moncada

You have the Width hardcoded, so it is always going to stay the same. Remove it, and change the alignment to stretch

你有 Width 硬编码,所以它总是会保持不变。删除它,并将对齐方式更改为拉伸

<TextBox Grid.Column="1" Margin="3" HorizontalAlignment="Stretch">

回答by Bence Végert

Just a note, if somebody facing with the same problem:

请注意,如果有人面临同样的问题:

For me the problem was that I use the SharedSizeGroup on the grid for both of my 2 columns. If i deleted the sharedsizegroup="b" on the columns what is *, the problem solved.

对我来说,问题是我在网格上为我的 2 列使用了 SharedSizeGroup。如果我删除了 * 列上的 sharedsizegroup="b",问题就解决了。

<StackPanel Orientation="Vertical"
            Grid.IsSharedSizeScope="True">
                        <Grid Margin="0 10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="a" />
                                <ColumnDefinition Width="*"  **SharedSizeGroup="b"**/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Size (m): " />
                            <TextBox x:Name="RealObjSize"
                                     Grid.Column="1"
                                     MinWidth="50"
                                     HorizontalAlignment="Stretch"
                                     TextChanged="RealObjSize_OnTextChanged" />
                        </Grid>

                        <Grid Margin="0 10">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="Auto" SharedSizeGroup="a" />
                                <ColumnDefinition Width="*" **SharedSizeGroup="b"**/>
                            </Grid.ColumnDefinitions>
                            <TextBlock Text="Distance (m): " />
                            <TextBox x:Name="RealObjDist"
                                 Grid.Column="1"
                                 MinWidth="50"
                                 HorizontalAlignment="Stretch"
                                 TextChanged="RealObjDist_OnTextChanged" />
                        </Grid>
                    </StackPanel>

回答by SyraKozZ

just put HorizontalAlignment="Stretch" and remove the Width

只需放置 Horizo​​ntalAlignment="Stretch" 并删除 Width