Wpf 如何获取文本框来填充可调整大小的列?
我正在尝试获取一个文本框来填充可调整大小的列中的可用空间。文本框是用户控件的一部分:Wpf 如何获取文本框来填充可调整大小的列?,wpf,textbox,stretch,splitter,Wpf,Textbox,Stretch,Splitter,我正在尝试获取一个文本框来填充可调整大小的列中的可用空间。文本框是用户控件的一部分: <UserControl x:Class="TextBoxLayout.FieldControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <Grid>
<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>
问题是文本框看起来很窄-我想让它填充左边的列并用拆分器调整大小。如何执行此操作?对FieldControl使用HorizontalAlignment=“Stretch”而不是“Left”。如果需要,删除MaxWidth。使用文本对齐来对齐文本。只需查看是否是您想要的
<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>
只是想为将来的代码搜索添加更多示例 我将其放在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}"/>
这将使文本居中,排序仍处于启用状态。文本框填充单元格,在本例中使用布尔变换器进行着色。您是指按宽度匹配,还是同时使用宽度和高度匹配?如果我不想使用MaxWidth,则可以使用此选项。然而。。。如果我添加MaxWidth(因为列变得非常宽,我不想让文本字段填充它),那么FieldControl将浮动在列的中心。如果我加回HorizontalAlignment=“Left”,那么我就回到了开始的位置。如何使文本字段在不填充的情况下与列的左侧对齐,并在拖动拆分器时处理列的收缩?我同意imekon的观点。这个答案让您回到了开始的地方。@anivas+1用于区分HorizontalAlignment和TextAlignment,以及同时使用两者的必要性。我忘了。