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

标签 wpf xaml

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

这是我的窗口

enter image description here

这是我的 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. 是否可以扩展文本框来填充第二列中的剩余空间?
  2. 是否可以使文本框随着表单大小的调整而调整?

最佳答案

您对宽度进行了硬编码,因此它始终保持不变。删除它,并将对齐方式更改为拉伸(stretch)

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

关于wpf - 如何使文本框扩展以填充网格单元的剩余空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25268910/

相关文章:

wpf - 鼠标悬停在 WPF 中

wpf - 绑定(bind)组上的绑定(bind)表达式列表为空的解决方法

c# - 将组合框显示文本设置为 ObservableCollection<T> 的属性

c# - WPF C# 数据绑定(bind)到 DataGridTextColumn

WPF 嵌套用户控件绑定(bind)

wpf - 在 listItem wpf 中将按钮右对齐

c# - 如何在不弄乱 DataContext 的情况下为 WPF 工具提示设置 PlacementTarget?

c# - 在 wpf 上设置自定义窗口位置

WPF 无法让命令和依赖属性一起工作,或者我只是用错了它们?

c# - 用户控件中具有 CompositeCollection 的 WPF ComboBox 不起作用 : SelectedIndex set to -1