【问题标题】:How do I get a TextBox to fill a resizable column?如何让 TextBox 填充可调整大小的列?
【发布时间】:2011-07-05 14:13:35
【问题描述】:

我正在尝试获取一个 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>

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

<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 看起来很窄 - 我想要它做的是填充左列并使用拆分器调整大小。我该怎么做?

【问题讨论】:

  • 你的意思是宽度适合,还是宽度和高度都适合?

标签: wpf textbox stretch splitter


【解决方案1】:

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

【讨论】:

  • 如果我不想拥有 MaxWidth,那就可以了。但是...如果我添加 MaxWidth(因为列变得非常宽并且我不希望文本字段填充它),那么 FieldControl 会浮动在列的中心。如果我加回 Horizo​​ntalAlignment="Left",那么我就回到了我开始的地方。如何让文本字段与列的左侧对齐而不填充它,并在拖动拆分器时处理列被缩小的问题?
  • 我同意imekon。这个答案让你回到你开始的地方。
  • @anivas +1 用于区分Horizo​​ntalAlignment 和TextAlignment,并需要同时使用两者。我忘记了。
【解决方案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}"/>

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

【讨论】:

    【解决方案3】:

    看看是不是你想要的

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-11-12
      • 2014-05-30
      • 2012-08-14
      • 1970-01-01
      • 1970-01-01
      • 2022-01-09
      • 2012-03-01
      相关资源
      最近更新 更多