简体   繁体   English

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

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

I have the following window with some input textboxes . 我有以下窗口和一些输入textboxes But these textboxes will not expand to fill the remaining space of the second column. 但是这些文本框不会扩展以填充第二列的剩余空间。 Furthermore when the window resizes the textboxes doesn't resize accordingly, 此外,当窗口调整大小时, textboxes不会相应调整大小

Here is my window 这是我的窗口

在此输入图像描述

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 textboxes to fill the remaining space in the second column? 是否有扩展textboxes以填充第二列中的剩余空间?
  2. Is there away to make the textboxes resize with the form resize? 有没有让textboxes调整大小调整大小?

You have the Width hardcoded, so it is always going to stay the same. 你有宽度硬编码,所以它总是保持不变。 Remove it, and change the alignment to stretch 将其取下,然后将对齐方式更改为拉伸

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

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. 对我来说问题是我在网格上为我的两列使用SharedSizeGroup。 If i deleted the sharedsizegroup="b" on the columns what is *, the problem solved. 如果我删除列上的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>

只需将Horizo​​ntalAlignment =“Stretch”并删除宽度即可

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

相关问题 如何使DevExpress GridControl列填充网格中的剩余空间? - How do I make a DevExpress GridControl column fill remaining space in the grid? WPF 如何使边框宽度扩大以填充 DockPanel 中的可用空间 - WPF How do I make a Border width expand to fill available space in a DockPanel 如何获得 ListView GridViewColumn 来填充网格中的剩余空间? - How can I get a ListView GridViewColumn to fill the remaining space in my grid? 无法获取TextBoxes(2)填充网格空间 - Cannot get TextBoxes(2) to fill grid space 让文本框填充网格中的剩余空间 - Let TextBox fill remaining Space in Grid 如何使 ItemsControl 拉伸以填充所有可用空间? - How do I make an ItemsControl stretch to fill all available space? 为什么WPF ComboBox的高度会扩展以填充网格单元,而不是在StackPanel中填充? - Why does a WPF ComboBox height expand to fill a grid cell, but not in a StackPanel? 如何使元素自动扩展以填充空间而不接管? - How to make an element auto-expand to fill space without taking over? 如何在工具栏中创建项目填充WPF中的所有可用空间 - How do I make an item in a toolbar fill all available space in WPF 如何在WPF中使文本框填充其他项目之间的水平空间? - How do I make a textbox in WPF fill the horizontal space between other items?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM