有没有办法如何告诉 _ wpf _ 中的组件占用100%的可用空间?
喜欢
width: 100%;
在CSS中
我有这个XAML,我不知道如何强制Grid占用100%的宽度。
<ListBox Name="lstConnections">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Background="LightPink">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Text="{Binding Path=User}" Margin="4"></TextBlock>
<TextBlock Grid.Row="0" Grid.Column="1" Text="{Binding Path=Password}" Margin="4"></TextBlock>
<TextBlock Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Text="{Binding Path=Host}" Margin="4"></TextBlock>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
结果看起来像
alt text http://foto.darth.cz/pictures/wpf_width.jpg
我把它做成了粉红色,这显然需要多少空间。我需要使粉红色网格100%宽度。
它是Grid
的容器,它的宽度很大。在这种情况下,这是一个ListBoxItem
,默认情况下是左对齐的。您可以将其设置为伸展,如下所示:
<ListBox>
<!-- other XAML omitted, you just need to add the following bit -->
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
您可以使用HorizontalContentAlignment="Stretch"
,如下所示:
<ListBox HorizontalContentAlignment="Stretch"/>