如何使文本框填充可调整大小的列?

23

我正在尝试让一个文本框填满可调整大小的列中的可用空间。该文本框是用户控件的一部分:

<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>

这个用户控件位于一个带有垂直分隔条的窗口中:

<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>

问题是TexTBox似乎非常狭窄 - 我想要的是它填充左列并随着分隔器调整大小。我该如何做到这一点?


您是指适合宽度还是宽度和高度都适合? - Alex Shtof
3个回答

55

使用 HorizontalAlignment="Stretch" 替代 "Left" 以适用于 FieldControl。如有需要,请删除 MaxWidth。使用 TextAlignment 以对齐文本。


5
如果我不想设置最大宽度,那么这个方法是可行的。但是......如果我添加了最大宽度(因为列变得非常宽,而我不希望文本框填充整个列),那么字段控件就会浮动到列的中心。如果重新添加HorizontalAlignment="Left",那么我又回到了起点。如何使文本框左对齐到列,并且在拖动分隔符时适应列缩小的情况? - imekon
我同意imekon的观点。这个回答让你回到了原点。 - Vaccano
@anivas +1 鉴于HorizontalAlignment和TextAlignment之间的区别以及需要同时使用两者,非常感谢您的提醒。我已经忘记了这一点。 - Kent Weigel

2

我想补充两个例子,以供日后代码搜索时参考。

我把这段代码放在XAML文件的顶部:

<UserControl.Resources>
    <Style TargetType="{x:Type TextBlock}" x:Key="CenterCell">
        <Setter Property="Background" Value="{Binding Included, Converter={StaticResource BoolToColorConverter}}"/>
        <Setter Property="HorizontalAlignment" Value="Stretch"/>
        <Setter Property="TextAlignment" Value="Center"/>
    </Style>
</UserControl.Resources>

然后在数据网格中:

<DataGridTextColumn Header="Excluded" Binding="{Binding Excluded}" ElementStyle="{StaticResource CenterCell}"/>

这将文本居中,同时保持排序功能。文本框填充单元格,使用bool转换器对其进行着色。


1

只需查看是否是您想要的

 <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"
                        Margin="2"
                         />

    <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>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接