I\'ve looked around for an answer on this, but the potential duplicates are more concerned with presentation than interaction.
I have a basic list box, and each ite
HorizontalContentAlignment="Stretch"
should be set in ItemContainerStyle for this to work.
Xaml Example
<ListBox xmlns:sys="clr-namespace:System;assembly=mscorlib">
<ListBox.ItemsSource>
<x:Array Type="{x:Type sys:String}">
<sys:String>String 1</sys:String>
<sys:String>String 2</sys:String>
<sys:String>String 3</sys:String>
<sys:String>String 4</sys:String>
</x:Array>
</ListBox.ItemsSource>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" Background="Green"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Update
Try this
<ListBox ...>
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
</Style>
</ListBox.ItemContainerStyle>
Also, adding to Fredrik's answer, if you set the ListBox's ScrollViewer.HorizontalScrollBarVisibility
property to Disabled
, the items will always have exactly the client width of the ListBox.