<ListBox ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<UniformGrid Rows="1" >
</UniformGrid>
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBoxItem HorizontalContentAlignment="Stretch">listbox item 1</ListBoxItem>
<ListBoxItem>listbox item 2</ListBoxItem>
<ListBoxItem>listbox item 3</ListBoxItem>
<ListBoxItem>listbox item 4</ListBoxItem>
<ListBoxItem>listbox item 5</ListBoxItem>
</ListBox>
I have added ListBoxItem
s as above and used UniformGrid
to display it. but I can't achieve content based width for each of the list box item. I have tried HorizontalContentAlignment
as Stretch
and Auto
width properties. Nothing happened.
You have to use
HorizontalAlignment
instead ofHorizontalContentAlignment
.If you want to set the alignment for all items, consider creating an item container style.
Please note that the default value for
HorizontalAlignment
is alreadyStretch
, so you will not see a difference there. Furthermore, there is noAuto
. The possible values areLeft
,Right
,Center
andStretch
. All values except forStretch
will size to the content.