ListBoxItem Horizo​​ntalContentAlignment 横跨整个 ListBox 的宽度 [英] ListBoxItem HorizontalContentAlignment To Stretch Across Full Width of ListBox

查看:14
本文介绍了ListBoxItem Horizo​​ntalContentAlignment 横跨整个 ListBox 的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 ListBoxItem 在 Windows Phone 8 应用程序上出现问题,同时试图让它们伸展到 ListBox 的所有宽度.

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​​ntalCONntentAlignment="Stretch" 但没有成功.我已经尝试了许多其他组合,似乎只有将边框或网格宽度设置为常量才有效,而另一种可行的解决方案是将边框宽度设置为绑定到包含列表框的实际宽度,但我想使用 Stretch变种,如果可以使它工作.

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.

推荐答案

我在 XAML 中遇到了同样的问题,这让我发疯了,想知道为什么我的 TextBlock 没有在宽度上完全着色.

I ran into the same in XAML and it drove me nuts wondering why my TextBlock was not fully colored across the width.

使用竞争样式的方法(这实际上适用于任何 xaml 变体)是明确定义 ListBoxItem 的样式以处理空间使用情况.

The way to work with the competing styles (this works for any of the xaml variants actually) is to define style of the ListBoxItem explicitly to handle the space usage.

这给了 xaml 一个提示,它以这种方式填充 (stretch) 到屏幕区域:

That gives the xaml a hint that it is to fill in (stretch) to the screen area in this way:

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

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

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

否则,默认情况下,xaml 解析器会尝试通过 auto 将其调整为 ListBoxItem 的内容来节省空间;给它那种可怕的透明胶带外观.

Otherwise the xaml parser, by default, tries to conserve space by auto sizing it to the contents of the ListBoxItem; giving it that dreaded scotch tape look.

这篇关于ListBoxItem Horizo​​ntalContentAlignment 横跨整个 ListBox 的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆