自定义的 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

原文地址:https://www.cnblogs.com/action98/p/3549479.html