375

我想让 ListItems 用它们的橙色背景扩展 Listbox 的整个宽度。

目前,它们仅与 FirstName + LastName 一样宽。

我已经将每个元素都设置为:Horizo​​ntalAlignment="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>
4

6 回答 6

661

我在这里找到了另一个解决方案,因为我遇到了两个帖子......

这是来自迈尔斯的回答:

<ListBox.ItemContainerStyle> 
    <Style TargetType="ListBoxItem"> 
        <Setter Property="HorizontalContentAlignment" Value="Stretch"></Setter> 
    </Style> 
</ListBox.ItemContainerStyle> 

这对我有用。

于 2010-05-27T19:30:46.520 回答
503

我确定这是重复的,但我找不到具有相同答案的问题。

添加HorizontalContentAlignment="Stretch"到您的列表框。这应该够了吧。请注意自动完成,因为它很容易HorizontalAlignment出错。

于 2009-05-08T08:44:12.617 回答
78

如果您的项目比更宽ListBox,这里的其他答案将无济于事:ItemTemplate仍然比ListBox.

对我有用的解决方法是禁用水平滚动条,显然,它还告诉所有这些项目的容器保持与列表框一样宽。

因此,获得与列表框一样宽的列表框项目的组合修复,无论它们更小并且需要拉伸,还是更宽并且需要换行,如下所示:

<ListBox HorizontalContentAlignment="Stretch" 
         ScrollViewer.HorizontalScrollBarVisibility="Disabled">

滚动条创意的学分

于 2012-08-23T16:44:00.010 回答
0

由于边框仅用于视觉外观,您可以将其放入 ListBoxItem 的 ControlTemplate 并在那里修改属性。在 ItemTemplate 中,您只能放置 StackPanel 和 TextBlock。这样,代码也保持干净,因为控件的外观将通过 ControlTemplate 控制,而要显示的数据将通过 DataTemplate 控制。

于 2015-02-08T22:53:16.947 回答
0

HorizontalAlignment="Stretch"对我来说,解决方法是在ItemsPresenter内部设置属性ScrollViewe..

希望这可以帮助某人...

<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>
于 2016-02-17T13:02:43.263 回答
0

我也遇到了同样的问题,作为一种快速解决方法,我使用 blend 来确定添加了多少填充。就我而言,它是 12,所以我使用负边距来摆脱它。现在一切都可以正确居中

于 2016-10-17T10:33:18.797 回答