【问题标题】:ListBoxItem HorizontalContentAlignment To Stretch Across Full Width of ListBoxListBoxItem Horizo​​ntalContentAlignment 横跨整个 ListBox 的宽度
【发布时间】:2013-05-17 19:09:13
【问题描述】:

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

我的ListBox

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

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

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

【问题讨论】:

标签: wpf xaml windows-phone-8 listbox listboxitem


【解决方案1】:

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

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

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

    <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 的内容来节省空间;给它那种可怕的透明胶带外观。

【讨论】:

  • 这是微软的bug吗?我浪费了三个小时才找到你的帖子。谢谢。
  • 它只是一个默认样式链,在独立控件中是有意义的,但是当它们组合在一起时给我们这个.... 欢迎来到发现这种样式复活节彩蛋的俱乐部。 ;-)
猜你喜欢
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 2010-10-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多