如何让 ListBox ItemTemplate 水平拉伸 ListBox 的整个宽度? [英] How to get a ListBox ItemTemplate to stretch horizontally the full width of the ListBox?
问题描述
我想让 ListItem 以橙色背景扩展列表框的整个宽度.
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.
我已将所有元素设置为:HorizontalAlignment="Stretch".
I've set every element I can to: HorizontalAlignment="Stretch".
我希望 ListboxItems 的背景在用户拉伸 Listbox 时扩展,因此我不想输入绝对值.
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.
我该怎么做才能让 ListBoxItems 的背景颜色填满 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>
推荐答案
我确定这是一个重复的问题,但我找不到答案相同的问题.
I'm sure this is a duplicate, but I can't find a question with the same answer.
将 HorizontalContentAlignment="Stretch"
添加到您的列表框.这应该够了吧.请小心使用自动完成功能,因为很容易错误地获取 HorizontalAlignment
.
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.
这篇关于如何让 ListBox ItemTemplate 水平拉伸 ListBox 的整个宽度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!