自定义的 ListBoxItem 自适应ListBox的宽度

时间:2023-03-09 04:22:28
自定义的 ListBoxItem 自适应ListBox的宽度

主要是要设置HorizontalContentAlignment的值,而不是HorizontalAlignment

 <ListBox x:Name="xxx">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Grid>
<TextBlock Text="www.tech-labs.net" HorizontalAlignment="Left"/>
<TextBlock Text="维思实验室" HorizontalAlignment="Right"/>
</Grid>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

@thl 2014年2月14日 13:55:24