C
C#10mo ago
DaVinki

❔ WPF TextBlock colored background no stretching with ListBox HorizontalContentAlignment

This is my XAML code for the list:
<!-- COM port list -->
<ListBox Grid.Row="1" Margin="6" HorizontalContentAlignment="Stretch" ItemsSource="{Binding ComPorts}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Background="LightGray" HorizontalAlignment="Center" FontSize="20" Height="30" TextAlignment="Center" FontWeight="Bold" Text="{Binding}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<!-- COM port list -->
<ListBox Grid.Row="1" Margin="6" HorizontalContentAlignment="Stretch" ItemsSource="{Binding ComPorts}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Background="LightGray" HorizontalAlignment="Center" FontSize="20" Height="30" TextAlignment="Center" FontWeight="Bold" Text="{Binding}"></TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Here is what the form looks like right now: I can't get the gray background to extend across the entire list item.
2 Replies
Klarth
Klarth10mo ago
Try to style the ListBoxItem instead to stretch it. https://stackoverflow.com/a/2924249/5348693
Stack Overflow
How to get a ListBox ItemTemplate to stretch horizontally the full ...
I want to have the ListItems to extend with their orange background the full width of the Listbox. Currently they are only as wide as the FirstName + LastName. I've set every element I can to:
Accord
Accord10mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.