如何让 ListBox ItemTemplate 水平拉伸 ListBox 的整个宽度?
我想让 ListItems 以其橙色背景扩展列表框的整个宽度。
目前,它们的宽度仅为名字 + 姓氏。
我已将所有元素设置为:HorizontalAlignment="Stretch"。
我希望 ListboxItems 的背景随着用户拉伸列表框而展开,因此我不想输入绝对值。
我必须做什么才能使 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 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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
对我来说,修复方法是在
ScrollViewe
r 内的ItemsPresenter
上设置属性HorizontalAlignment="Stretch"
。希望这对某人有帮助...
The fix for me was to set property
HorizontalAlignment="Stretch"
onItemsPresenter
insideScrollViewe
r..Hope this helps someone...
我也遇到了同样的问题,作为快速解决方法,我使用混合来确定添加了多少填充。 在我的例子中,它是 12,所以我使用负边距来消除它。 现在一切都可以正确居中了
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
我在这里找到了另一个解决方案,因为我遇到了这两篇文章......
这是来自迈尔斯回答:
这对我有用。
I found another solution here, since I ran into both post...
This is from the Myles answer:
This worked for me.
我确信这是重复的,但我找不到具有相同答案的问题。
将
HorizontalContentAlignment="Stretch"
添加到列表框。 这应该够了吧。 请小心自动完成,因为很容易错误地获取HorizontalAlignment
。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 getHorizontalAlignment
by mistake.如果您的项目比
ListBox
宽,则此处的其他答案将无济于事:ItemTemplate
中的项目仍然比ListBox 宽>列表框
。对我有用的修复方法是禁用水平滚动条,显然,这也告诉所有这些项目的容器仅保持与列表框一样宽。
因此,获取与列表框一样宽的 ListBox 项目的组合修复,无论它们较小且需要拉伸,还是较宽且需要换行,如下所示:
(滚动条创意的制作人员)
If your items are wider than the
ListBox
, the other answers here won't help: the items in theItemTemplate
remain wider than theListBox
.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:
(credits for the scroll bar idea)
由于边框仅用于视觉外观,因此您可以将其放入 ListBoxItem 的 ControlTemplate 中并修改其中的属性。 在 ItemTemplate 中,您只能放置 StackPanel 和 TextBlock。 通过这种方式,代码也保持干净,因为控件的外观将通过 ControlTemplate 控制,要显示的数据将通过 DataTemplate 控制。
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.