ListBoxItem Horizo​​ntalContentAlignment 在 ListBox 的整个宽度上拉伸 [英] ListBoxItem HorizontalContentAlignment To Stretch Across Full Width of ListBox

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

问题描述

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

我的ListBox:

并且它的 DataTemplates 在一个单独的 xaml 资源文件中:

<Border Horizo​​ntalAlignment="Stretch"><网格高度="50"><Grid.ColumnDefinitions><ColumnDefinition Width="*"/><ColumnDefinition Width="6*"/></Grid.ColumnDefinitions><Image Grid.Column="0" Source="{Binding ImageUri}" VerticalAlignment="Center" Horizo​​ntalAlignment="Center" Height="30"/><TextBlock Grid.Column="1" Text="{Binding SomeText}" VerticalAlignment="Center" Horizo​​ntalAlignment="Left" Foreground="Black"/></网格></边框></数据模板>

我无法让物品真正拉伸,我不知道问题出在哪里.我试图设置 ItemContainerStyle Horizo​​ntalCONtentAlignment="Stretch" 但它没有用.我尝试了许多其他组合,似乎只有将边框或网格宽度设置为常量,而另一种可行的解决方案是将边框宽度设置为绑定到包含列表框的 ActualWidth,但我想使用 Stretch变体,如果可以使它工作.

解决方案

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

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

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

 </风格></ListBox.ItemContainerStyle><ListBox.ItemTemplate>...</ListBox.ItemTemplate>

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

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.

My ListBox:

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

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>

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.

解决方案

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

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.

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>

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天全站免登陆