如何获取TextBox以填充可调整大小的列?

ime*_*kon 17 wpf textbox splitter stretch

我正在尝试使用TextBox来填充可调整大小的列中的可用空间.TextBox是用户控件的一部分:

<UserControl x:Class="TextBoxLayout.FieldControl"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Label Grid.Column="0">Name</Label>
        <TextBox Grid.Column="1"/>
    </Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)

此用户控件位于带垂直拆分器的窗口中:

<Window x:Class="TextBoxLayout.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TextBoxLayout"
    Title="Text box layout" Height="400" Width="600">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition MinWidth="100"/>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition MinWidth="100"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition/>
        </Grid.RowDefinitions>

        <local:FieldControl Grid.Column="0" Grid.Row="0" MaxWidth="200" HorizontalAlignment="Left"/>

        <TextBlock Grid.Column="0" Grid.Row="1" Text="Testing"/>

        <GridSplitter Grid.Column="1" Grid.Row="0" Grid.RowSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="3"/>

        <TextBlock Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" Text="Testing"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

问题是TexTBox似乎非常狭窄 - 我想要它做的是填充左列并使用拆分器调整大小.我怎么做?

ani*_*vas 40

对FieldControl使用Horizo​​ntalAlignment ="Stretch"而不是"Left".如果需要,删除MaxWidth.使用TextAlignment对齐文本.

  • 如果我不想拥有MaxWidth,那就有效.但是...如果我添加MaxWidth(因为列变得非常宽并且我不希望文本字段填充它),则FieldControl将浮动在列的中心.如果我加回Horizo​​ntalAlignment ="Left",那么我就回到我开始的地方.如何在不填充列的情况下将文本字段对齐到列的左侧,并在拖动拆分器时应对要缩小的列? (4认同)