ListBoxItem中Horizo​​ntalContentAlignment要横跨全宽列表框的列表、ListBoxItem、Horizo、ntalContentAlignment

2023-09-11 07:45:32 作者:踩着棺材跳鬼步

我有我的 ListBoxItem的的基于Windows Phone 8的应用程序有问题,而试图让他们横跨的列表框。

I have a problem with my ListBoxItem's on a Windows Phone 8 app, while trying to get them to stretch across all the width of the ListBox.

我的列表框

<ListBox 
      ItemsSource="{Binding Events}" 
      behaviors:ItemClickCommandBehavior.Command="{Binding EventSelectedCommand}"
      ItemTemplate="{StaticResource EventListTemplateSelector}"/>

和它的DataTemplates是在一个单独的XAML资源文件:

And its DataTemplates are in a seperate xaml resource file:

<DataTemplate x:Key="EventListHeaderTemplate">
    <Border HorizontalAlignment="Stretch">
        <Grid Height="50">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="6*"/>
            </Grid.ColumnDefinitions>
            <Image Grid.Column="0" Source="{Binding ImageUri}" VerticalAlignment="Center" HorizontalAlignment="Center" Height="30"/>
            <TextBlock Grid.Column="1" Text="{Binding SomeText}" VerticalAlignment="Center" HorizontalAlignment="Left" Foreground="Black"/>
        </Grid>
    </Border>
</DataTemplate>

我不能让项目真正舒展了,我不知道问题出在哪里。我试图设置 ItemContainerStyle Horizo​​ntalCOntentAlignment =拉伸,并没有奏效。我曾尝试过许多其他的组合,似乎只设置边框或网格宽度恒定的作品和一个工程是设置边框宽度绑定到包含ListBox的ActualWidth的其他解决办法,但我想用拉伸如果变量可以使它发挥作用。

I cant get the items to really Stretch, and I dont know where the problem is. I have tried to set the ItemContainerStyle HorizontalCOntentAlignment="Stretch" and it didn't work. I have tried many other combinations and it seems that only setting the Border or Grid width to a constant works and one other solution that works is to set the Border width to bind to the ActualWidth of the containing ListBox, but I want to use the Stretch variant if could make it work.

推荐答案

我跑进在WP8相同的,它驱使我坚果。解决方法是把它定义这种方式

I ran into the same on WP8 and it drove me nuts. The work around is to define it this way

    <ListBox Name="lbTest" HorizontalContentAlignment="Stretch"  >

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

        <ListBox.ItemTemplate>...</ListBox.ItemTemplate>