By default, a ListBoxItem does not fill in the space that it sets. He aligns himself and the contents on the left. For the contents of your ListBoxItem to span the entire width, you need to change the ItemContainerStyle
<ListBox> <ListBox.ItemContainerStyle> <Style TargetType="ListBoxItem"> <Setter Property="HorizontalContentAlignment" Value="Stretch"/> </Style> </ListBox.ItemContainerStyle> </ListBox>
Now the content will cover the available width. If you want to use the StackPanel, as in your example, be sure to set HorizontalAlignment. StackPanel also does not fill the available space indicated by
<DataTemplate> <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch"> <TextBlock Text="abc" HorizontalAlignment="Left" VerticalAlignment="Center"/> <TextBlock Text="def" HorizontalAlignment="Right" VerticalAlignment="Center"/> </StackPanel> </DataTemplate>
However, I would recommend using a Grid and defining two columns
<DataTemplate> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <TextBlock Text="abc" HorizontalAlignment="Left" VerticalAlignment="Center"/> <TextBlock Text="def" Grid.Column="1" HorizontalAlignment="Right" VerticalAlignment="Center"/> </Grid> </DataTemplate>
source share