简体   繁体   English

Windows Phone用户控件不会在ListBox中拉伸

[英]Windows Phone User Control does not stretch in ListBox

I want the items inside ListBox to fill all allotted space. 我希望ListBox中的项目填充所有分配的空间。 But they don't. 但他们没有。 They use only the amount of space they need. 他们只使用他们需要的空间。

ListBox looks as follows: 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>

And the user control that is bound as ListBox Item: 并且绑定为ListBox项的用户控件:

<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>

Am I making any mistake? 我犯了什么错吗?

You need to set the HorizonatalContentAlignment to Stretch . 您需要将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>

This is because otherwise the content just takes up the space it needs rather than using all the space that's available. 这是因为否则内容只占用它所需的空间而不是使用所有可用的空间。

设置HorizontalContentAlignmentListBoxItems已拉伸,其内容不会。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM