Xamarin.Forms:StackLayout内的ListView:如何设置高度? [英] Xamarin.Forms: ListView inside StackLayout: How to set height?

查看:319
本文介绍了Xamarin.Forms:StackLayout内的ListView:如何设置高度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ContentPage中,我在ScrollView内的StackLayout内有ListView.当调用OnAppearing时,在ContentPage中填充了ListView(设置了ItemSource),我可以看到该列表已在模拟器中填充. StackLayout的方向是Vertical,在ListView之下,我有Button.

In a ContentPage I have a ListView inside a StackLayout inside a ScrollView. The ListView is populated (ItemSource is set) in the ContentPage when OnAppearing gets called and I can see that the list is populated in the emulator. The StackLayouts orientation is Vertical and below the ListView I have a Button.

我的问题是,无论ListView包含多少个元素,它的高度都为53.33.我希望ListView的高度与其中项目的总高度相匹配.通过设置HeightRequest,我可以将ListView的高度设置为我想要的任何值,但是由于我不知道ListView内项目的高度,因此结果通常是到其下方按钮的距离为不正确,因此看起来很丑.我已经尝试将ListViewStackLayout上的VerticalOptions都设置为Start和其他设置,但是这不会更改53.33的高度(如果我尝试结合使用HeightRequest结果证明HeightRequest胜出了.

My problem is that no matter how many elements the ListView has, it gets the height of 53.33. I would like the height of the ListView to match the total height of the items in it. By setting HeightRequest I can set the height of the ListView to anything I want, but since I do not know the height of the items inside the ListView the result is most often that the distance to the button below it is incorrect and therefore looks ugly. I have tried to set VerticalOptions on both the ListView and the StackLayout to Startand other settings, but this does not change the height from 53.33 (and if I try to combine using HeightRequest and Start it turns out that HeightRequest wins out).

我该如何解决?

(请原谅来自 Xamarin论坛)

推荐答案

在我的案例中,解决方案是将ListView放在StackLayout内,然后将StackLayout放在主StackLayout内.然后,我可以在内部StackLayout(包含ListView的那个)上设置VerticalOptions = LayoutOptions.FillAndExpand,结果是ListView获得了所需的空间(当然,该空间根据数据而有所不同).

The solution in my case was to put the ListView inside a StackLayout and then put that StackLayout inside the main StackLayout. Then I could set the VerticalOptions = LayoutOptions.FillAndExpand on the inner StackLayout (the one containing the ListView) with the result that the ListView got the space it needed (which of course varies depending on the data).

这是主要代码:

listView.ItemsSource = alternativeCells;
listView.ItemSelected += ListViewOnItemSelected;
var listStackLayout = new StackLayout
{
    VerticalOptions = LayoutOptions.FillAndExpand,
    Orientation = StackOrientation.Vertical
};
listStackLayout.Children.Add(listView);
_stackLayout.Children.Add(listStackLayout);

如您所见,我添加了一个新的StackLayout,其唯一目的是将ListView放入其中.然后,将listStackLayout放入主_stackLayout中.

As you see, I added a new StackLayout with the only purpose of putting the ListView inside it. Then I put that listStackLayout inside the main _stackLayout.

有关更多信息,请参见此Xamarin论坛帖子上的帖子

See the post on this Xamarin forum post for more information

这篇关于Xamarin.Forms:StackLayout内的ListView:如何设置高度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆