问题描述
我有一个 LongListSelector
,它位于 StackPanel
内.当这个 LLS 为空时,它的长度是无限的,并且看不到它底部的元素.
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>
但是当我将它设置为 ItemsSource
时,它会变得很好.我尝试将它的 VerticalAlignment
分配到顶部,但没有解决问题如何让它的尺寸不填表格?
but when I set it's ItemsSource
, it gets fine. I tried assigning it's VerticalAlignment
to top, but didn't solved the issueHow to make it's size not fill the form?
推荐答案
(我已经编辑了这篇文章以使其更好)
(I've edited this post to make it better)
首先让我们描述您遇到的问题,为此我们将使用:
First of all lets describe the problem you have, to do it we will use:
问题:LongListSelector (LLS) 的无限长度 - 老实说,这不是问题,它的工作原理应该如此.因为 LLS 可以有很多很多项目,而且正如它的名字所说的可能很长.问题是您在 StackPanel 中使用它而不固定其高度.
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.
解决方案:
第一个很简单 - 只需设置 LLS 的高度.您将确定应该低于 LLS 的内容将在那里.就像@Chris W 提到的那样——在 StackPanel 中使用 LLS 并不是最幸运的,并且会导致很多问题——所以要避免它.
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>
最优雅和最好的解决方案(也是@Chris W 建议的)- 将您的 LLS 放入 Grid.这种方式有很多优点,并且有了 Rowdefinitions,您的程序将独立于电话分辨率 - 您的所有控件都在那里,如果它们应该在那里.
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>
第三个解决方案不如之前的广告那么好,但展示了管理控件的其他方式.您可以覆盖测量 LLS 的方式.但是使用这种方法时,您必须注意,例如:它可以解决问题,除非您添加太多项目,否则您的控件将被推出屏幕.此外,您还必须注意必须定义的 this.Width.这么多额外的条件你必须检查,当然你可以添加更多的修改,它会起作用,但正如我所说,它不如以前的解决方案.
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);
}
}
}
在您的 xaml 中,您必须添加:
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>
这篇关于空 LongListSelector 具有无限长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!