空LongListSelector具有无限长度

时间:2013-11-13 20:01:01

标签: xaml windows-phone-7 windows-phone-8 windows-phone longlistselector

我的LongListSelector位于StackPanel内。当此LLS为空时,它具有无限长度,并且无法看到位于其底部的元素。

<StackPanel Orientation="Vertical">
    <phone:LongListSelector>
    </phone:LongListSelector>
</StackPanel>

但是当我设置它ItemsSource时,它会好起来的。我尝试将它VerticalAlignment分配到顶部,但没有解决问题 如何使它的大小不填写表格?

1 个答案:

答案 0 :(得分:1)

(我已编辑此帖以使其更好)

首先让我们描述你遇到的问题,为此我们将使用:                            

问题:LongListSelector(LLS)的无限长度 - 说实话,它不是问题而且它的工作方式应该如此。因为LLS可以有许多项目,而且名称可以很长。问题是你在StackPanel中使用它而不修复它的高度。


  1. 第一个非常简单 - 只需设置LLS的高度即可。你会确定LLS以下应该是什么。就像@Chris W提到的那样 - 在StackPanel中使用LLS并不是最乐观的,并且会引起很多问题 - 所以要避免它。

    <StackPanel Orientation="Vertical">
        <phone:LongListSelector Height="300"/>
        <TextBlock Text="Something/>
    </StackPanel>
    
  2. 最优雅,最好的解决方案(也是@Chris W建议的) - 将您的LLS放入网格。这种方式有很多优点,使用Rowdefinitions,你的程序将独立于手机分辨率 - 所有的控件都将存在,如果应该的话。

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="8*"/>
            <RowDefinition Height="2*"/>
        </Grid.RowDefinitions>
        <phone:LongListSelector Width="100" Grid.Row="0"/>
        <TextBlock Text="Something" Grid.Row="1"/>
    </Grid>
    
  3. 第三个解决方案不是以前的好广告,但显示了管理您的控件的其他方法。您可以覆盖LLS的测量方式。但是使用这种方法你必须注意例如:它可以解决问题,除非你添加了很多项目,你的控件将被推离屏幕。此外,你必须注意这个。宽度,必须定义。你必须检查许多其他条件,当然你可以添加更多修改,它会起作用,但正如我所说,它不如以前的解决方案。

    namespace Extensions
    {
       public class LongListSelectorEx : LongListSelector
       {
          protected override System.Windows.Size MeasureOverride(System.Windows.Size availableSize)
          {
             if (this.ItemsSource == null)
                return new System.Windows.Size(this.Width, 0);
             if (this.ItemsSource.Count <= 0)
                return new System.Windows.Size(this.Width, 0);
    
             return base.MeasureOverride(availableSize);
          }
       }
    }
    
  4. 在您的xaml中,您必须添加:

    <phone:PhoneApplicationPage
     // something before
     xmlns:common="clr-namespace:Extensions"
     // other things
     >
    
    <StackPanel Orientation="Vertical">
        <common:LongListSelectorEx Width="200"/>
        <TextBlock Text="Something/>
    </StackPanel>
    
相关问题