问题
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: HorizontalAlignment=\"Stretch\".
I want the background of the ListboxItems to expand as the user stretches the Listbox so I don\'t want to put in absolute values.
What do I have to do so that the background color of the ListBoxItems fill the width of the ListBox?
<Window x:Class=\"TestListBoxSelectedItemStyle.Window1\"
xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"
xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\"
xmlns:local=\"clr-namespace:TestListBoxSelectedItemStyle\"
Title=\"Window1\" Height=\"300\" Width=\"300\">
<Window.Resources>
<local:CustomerViewModel x:Key=\"TheDataProvider\"/>
<DataTemplate x:Key=\"CustomerItemTemplate\">
<Border CornerRadius=\"5\" Background=\"Orange\" HorizontalAlignment=\"Stretch\" Padding=\"5\" Margin=\"3\">
<StackPanel Orientation=\"Horizontal\" HorizontalAlignment=\"Stretch\" Width=\"Auto\">
<TextBlock HorizontalAlignment=\"Stretch\">
<TextBlock.Text>
<MultiBinding StringFormat=\"{}{0} {1}\">
<Binding Path=\"FirstName\"/>
<Binding Path=\"LastName\"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox ItemsSource=\"{Binding Path=GetAllCustomers, Source={StaticResource TheDataProvider}}\"
ItemTemplate=\"{StaticResource CustomerItemTemplate}\"/>
</Grid>
</Window>
回答1:
I'm sure this is a duplicate, but I can't find a question with the same answer.
Add HorizontalContentAlignment="Stretch"
to your ListBox. That should do the trick. Just be careful with auto-complete because it is so easy to get HorizontalAlignment
by mistake.
回答2:
I found another solution here, since I ran into both post...
This is from the Myles answer:
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter>
</Style>
</ListBox.ItemContainerStyle>
This worked for me.
回答3:
If your items are wider than the ListBox
, the other answers here won't help: the items in the ItemTemplate
remain wider than the ListBox
.
The fix that worked for me was to disable the horizontal scrollbar, which, apparently, also tells the container of all those items to remain only as wide as the list box.
Hence the combined fix to get ListBox items that are as wide as the list box, whether they are smaller and need stretching, or wider and need wrapping, is as follows:
<ListBox HorizontalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled">
(credits for the scroll bar idea)
回答4:
Since the border is used just for visual appearance, you could put it into the ListBoxItem's ControlTemplate and modify the properties there. In the ItemTemplate, you could place only the StackPanel and the TextBlock. In this way, the code also remains clean, as in the appearance of the control will be controlled via the ControlTemplate and the data to be shown will be controlled via the DataTemplate.
回答5:
The fix for me was to set property HorizontalAlignment="Stretch"
on ItemsPresenter
inside ScrollViewe
r..
Hope this helps someone...
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListBox">
<ScrollViewer x:Name="ScrollViewer" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Foreground="{TemplateBinding Foreground}" Padding="{TemplateBinding Padding}" HorizontalAlignment="Stretch">
<ItemsPresenter Height="252" HorizontalAlignment="Stretch"/>
</ScrollViewer>
</ControlTemplate>
</Setter.Value>
</Setter>
回答6:
I also had the same problem, as a quick workaround, I used blend to determine how much padding was being added. In my case it was 12, so I used a negative margin to get rid of it. Now everything can now be centered properly
来源:https://stackoverflow.com/questions/838828/how-to-get-a-listbox-itemtemplate-to-stretch-horizontally-the-full-width-of-the