最新发现填充ListBox的item时容器不能自动填满空间,如使用
<DataTemplate x:Key="ItemTemplate">
<Grid Height="50">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>
<TextBlock Text="{Binding name}" VerticalAlignment="Center"/>
<TextBlock Text="{Binding age}" VerticalAlignment="Center"/>
</Grid>
</DataTemplate>
作为ListBox的ItemTemplate,该模板容器不会水平填满ListBox的所占的水平空间。要解决该问题只需将ListBox的ItemContainerStyle设置为
<Style x:Key="ListBoxItemStyle" TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="HorizontalAlignment" Value="Stretch"/>
</Style>
即可。