LongListSelector内容与父级不匹配

时间:2015-10-03 00:34:20

标签: c# windows-phone-8 windows-phone longlistselector

我目前正在使用Windows Phone 8应用程序,其中我使用LongListSelector控件。

我创建了一个ItemTemplate,其中Grid为容器,一些TextBlock为子容器。我不知道原因,但Grid容器与LongListSelector控件不匹配。

这里是我使用的代码。我为LongListSelector控件使用蓝色背景,为Grid控件使用红色背景以突出显示问题:

<phone:LongListSelector 
  HorizontalContentAlignment="Stretch"
  HorizontalAlignment="Stretch"
  ItemsSource="{Binding Data}" 
  Background="Aqua"
>
  <phone:LongListSelector.ItemTemplate>
    <DataTemplate>
      <Grid
        HorizontalAlignment="Stretch"
        Background="Red"
      >
        <Grid.ColumnDefinitions>
          <ColumnDefinition Width="25" />
          <ColumnDefinition Width="75" />
          <ColumnDefinition Width="*" />
          <ColumnDefinition Width="50" />
          <ColumnDefinition Width="47" />
          <ColumnDefinition Width="100" />
          <ColumnDefinition Width="45" />
          <ColumnDefinition Width="35" />
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Column="0" Style="{StaticResource LineStyle}" Text="{Binding Value}" />
        <TextBlock Grid.Column="1" Style="{StaticResource LineStyle}" Text="{Binding Value}" />
        <TextBlock Grid.Column="2" Style="{StaticResource LineStyle}" Text="{Binding Value}" TextAlignment="Left" TextTrimming="WordEllipsis" />
        <TextBlock Grid.Column="3" Style="{StaticResource LineStyle}" Text="{Binding Value}" />
        <TextBlock Grid.Column="4" Style="{StaticResource LineStyle}" Text="{Binding Value}" />
        <TextBlock Grid.Column="5" Style="{StaticResource LineStyle}" Text="{Binding Value}" />
        <TextBlock Grid.Column="6" Style="{StaticResource LineStyle}" Text="{Binding Value}" />
        <TextBlock Grid.Column="7" Style="{StaticResource LineStyle}" Text="{Binding Value}"  />
      </Grid>
    </DataTemplate>
  </phone:LongListSelector.ItemTemplate>
</phone:LongListSelector>

这里的风格:

<Style x:Key="LineStyle" TargetType="TextBlock">
  <Setter Property="Foreground" Value="Black" />
  <Setter Property="FontSize" Value="13" />
  <Setter Property="Margin" Value="0, 12" />
  <Setter Property="TextAlignment" Value="Center" />
  <Setter Property="HorizontalAlignment" Value="Stretch" />
</Style>

这里有截图:

enter image description here

如何强制Grid容器(红色的容器)与其父容器匹配?

提前感谢您的帮助!

1 个答案:

答案 0 :(得分:1)

我认为这是由于LongListSelector的垂直ScrollBar,它在列表控件的右侧添加了空格。

您可以通过在页面的init中添加以下代码来删除此右侧空格。

MyList.Loaded += (sender, e) => { var scrollBar = ((FrameworkElement)VisualTreeHelper.GetChild(MyList, 0)).FindName("VerticalScrollBar") as ScrollBar; scrollBar.Margin = new Thickness(-10, 0, 0, 0); };