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

发布于 2024-11-18 10:55:53 字数 1846 浏览 2 评论 0原文

我正在尝试获取一个文本框来填充可调整大小的列中的可用空间。 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 看起来非常窄 - 我希望它做的是填充左列并使用拆分器调整大小。我该怎么做?

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

空气里的味道 2024-11-25 10:55:53

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

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

孤独患者 2024-11-25 10:55:53

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

我将其放在 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}"/>

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

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

I put this in the top of the xaml file:

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

长伴 2024-11-25 10:55:53

看看是否是您想要的

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

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文