Empty LongListSelector has infinite length

柔情痞子 提交于 2019-12-12 06:45:35

问题


I have a LongListSelector which is inside a StackPanel. when this LLS is empty, it has infinite length and elements which are at the bottom of it can't be seen.

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

but when I set it's ItemsSource, it gets fine. I tried assigning it's VerticalAlignment to top, but didn't solved the issue How to make it's size not fill the form?


回答1:


(I've edited this post to make it better)

First of all lets describe the problem you have, to do it we will use:

PROBLEM: infinite length of LongListSelector (LLS)- to be honest, it isn't a problem and it works like it should. Because LLS can have many many items and can be very long as its name says. The problem is that you use it in StackPanel without fixing its Height.

SOLUTIONS:

  1. The first is very simple - just set the height of LLS. You will be sure that what should be below LLS, will be there. Like @Chris W mentioned - using LLS in StackPanel is not most forunate and can cause many problems - so avoid it.

    <StackPanel Orientation="Vertical">
        <phone:LongListSelector Height="300"/>
        <TextBlock Text="Something/>
    </StackPanel>
    
  2. The most elegant and the best solution (also what @Chris W suggested) - to put your LLS into Grid. That way has many advantages and with Rowdefinitions your program will be independent of Phone resolution - all your controls will be there, were they should be.

    <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. The third solution is not as good ad previous, but shows other way to manage your Controls. You can override the way LLS is measured. But with this method you have to watch out for example: it will work ok with the problem, unless you add so many items that your Controls will be pushed off the screen. Also you will have to watch out for this.Width, which has to be defined. So many additional conditions you have to check, of course you can add more modifications and it will work, but as I said it's not as good as previous solutions.

    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);
          }
       }
    }
    

In your xaml you have to add:

<phone:PhoneApplicationPage
 // something before
 xmlns:common="clr-namespace:Extensions"
 // other things
 >

<StackPanel Orientation="Vertical">
    <common:LongListSelectorEx Width="200"/>
    <TextBlock Text="Something/>
</StackPanel>


来源:https://stackoverflow.com/questions/19963187/empty-longlistselector-has-infinite-length

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!