wpf 如何让 TextBox 填充可调整大小的列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6584100/
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 22:57:36  来源:igfitidea点击:

How do I get a TextBox to fill a resizable column?

wpftextboxstretchsplitter

提问by imekon

I'm trying to get a TextBox to fill the available space in a resizable column. The TextBox is part of a user control:

我正在尝试使用 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>

This user control is in a window with a vertical splitter:

此用户控件位于带有垂直分隔符的窗口中:

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

The problem is the TexTBox appears to be very narrow - what I'd want it to do is fill the left column and resize with the splitter. How do I do that?

问题是 TexTBox 看起来很窄 - 我希望它做的是填充左列并使用拆分器调整大小。我怎么做?

回答by anivas

Use HorizontalAlignment="Stretch" instead of "Left" for FieldControl. Remove MaxWidth if required. Use TextAlignment to align text.

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

回答by Rahbek

Just wanted to add to more examples for future code searches.

只是想为将来的代码搜索添加更多示例。

I put this in the top of the xaml file:

我把它放在 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>

And then in the datagrid:

然后在数据网格中:

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

This centers the text and sorting is still enabled. The textbox fills the cell and in this case is colored using a bool converter.

这使文本居中并且仍然启用排序。文本框填充单元格,在这种情况下使用布尔转换器着色。

回答by Kishore Kumar

just see whether is that you want

看看是不是你想要的

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