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

猜你喜欢

转载自www.cnblogs.com/GarsonZhang/p/12274815.html