【问题标题】:Windows Phone User Control does not stretch in ListBoxWindows Phone 用户控件不会在 ListBox 中拉伸
【发布时间】:2012-01-08 18:08:43
【问题描述】:

我希望 ListBox 中的项目填满所有分配的空间。但他们没有。他们只使用他们需要的空间。

ListBox 如下所示:

<ListBox x:Name="StripesList" Grid.Row="1">
  <ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
      <Setter Property="HorizontalAlignment" Value="Stretch"/>
      <Setter Property="Margin" Value="0,4,0,4"/>
    </Style>
  </ListBox.ItemContainerStyle>
</ListBox>

以及绑定为ListBox Item的用户控件:

<UserControl x:Class="Blip.UI.Controls.StatusStrip"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    HorizontalAlignment="Stretch">

    <Grid HorizontalAlignment="Stretch">
           <Grid.RowDefinitions>
               <RowDefinition Height="*" MinHeight="76" />
               <RowDefinition Height="20" />
           </Grid.RowDefinitions>
           <Grid.ColumnDefinitions>
               <ColumnDefinition Width="64" />
               <ColumnDefinition Width="*" />
           </Grid.ColumnDefinitions>

           <Image Grid.Column="0" Grid.Row="0" Width="64" Height="64"/>

           <TextBlock Grid.Column="1" Grid.Row="0" Text="Test"/>

           <Grid Grid.ColumnSpan="2" Grid.Row="1">
               <StackPanel Orientation="Horizontal" VerticalAlignment="Bottom">
                   <Image Source="/Resources/1.png" Width="18" Height="18"/>
                   <TextBlock Text="Test" VerticalAlignment="Center" />
               </StackPanel>
           </Grid>
    </Grid>
</UserControl>

我有什么错误吗?

【问题讨论】:

    标签: wpf xaml windows-phone-7 layout windows-phone-7.1


    【解决方案1】:

    您需要将HorizonatalContentAlignment 设置为Stretch

    <ListBox x:Name="StripesList" Grid.Row="1">
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Setter Property="HorizontalAlignment" Value="Stretch"/>
          <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
          <Setter Property="Margin" Value="0,4,0,4"/>
        </Style>
      </ListBox.ItemContainerStyle>
    </ListBox>
    

    这是因为否则内容只会占用它需要的空间,而不是使用所有可用空间。

    【讨论】:

      【解决方案2】:

      设置HorizontalContentAlignmentListBoxItems已经拉伸,它们的内容没有。

      【讨论】:

        猜你喜欢
        • 2018-08-19
        • 2017-02-27
        • 1970-01-01
        • 1970-01-01
        • 2017-02-26
        • 1970-01-01
        • 2011-06-22
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多