我正在使用 StackLayout 和 ListView 来显示 View 的某些部分,但是 ListView 占用的空间比我需要的多,并且在列表的最后一行和配置文件延续之间留有空白。似乎我的 ListView 的行数比实际列表的长度多,或者它的高度是固定的,我不知道……这是我的 XAML:
<ScrollView>
<StackLayout Orientation="Vertical" Spacing="10">
<Label Text="{Binding Establishment.Name}" TextColor="Black" HorizontalOptions="StartAndExpand" Style="{DynamicResource TitleStyle}" FontAttributes="Bold"/>
<Label Text="{Binding Establishment.Category.Name, StringFormat='Categoria: {0}'}" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
<Label Text="{Binding Establishment.Description}" TextColor="Black" HorizontalOptions="StartAndExpand" LineBreakMode="WordWrap" XAlign="Start"/>
<Label Text="Contatos" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
<ListView ItemsSource="{Binding Establishment.Contacts}" VerticalOptions="Start" HasUnevenRows="True">
<ListView.ItemTemplate>
<DataTemplate>
<TextCell Text="{Binding Value}" Detail="{Binding StringType}" DetailColor="Gray"/>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<Label Text="Endereço" TextColor="Black" HorizontalOptions="StartAndExpand" FontAttributes="Bold"/>
<Label Text="{Binding Establishment.FullAddress}" TextColor="Black" HorizontalOptions="StartAndExpand" />
<Button Text="Ver no mapa"/>
</StackLayout>
</ScrollView>
我该如何解决?我看到人们使用 HasUnevenRows="True"
但它对我不起作用。
最佳答案
正如 hvaughan3 所说,这确实不是一个好的做法。但我有同样的情况,我使用了行为的解决方法:
public class ListViewHeightBehavior : Behavior<ListView>
{
private ListView _listView;
public static readonly BindableProperty ExtraSpaceProperty =
BindableProperty.Create(nameof(ExtraSpace),
typeof(double),
typeof(ListViewHeightBehavior),
0d);
public double ExtraSpace
{
get { return (double)GetValue(ExtraSpaceProperty); }
set { SetValue(ExtraSpaceProperty, value); }
}
protected override void OnAttachedTo(ListView bindable)
{
base.OnAttachedTo(bindable);
_listView = bindable;
_listView.PropertyChanged += (s, args) =>
{
var count = _listView.ItemsSource?.Count();
if (args.PropertyName == nameof(_listView.ItemsSource)
&& count.HasValue
&& count.Value > 0)
{
_listView.HeightRequest = _listView.RowHeight * count.Value + ExtraSpace;
}
};
}
}
XAML:
<ListView ItemsSource="{Binding Items}"
HasUnevenRows="True">
<ListView.Behaviors>
<behaviors:ListViewHeightBehavior ExtraSpace="180"/>
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
// Your template
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
关于c# - ListView 中的空间超出了我的需要,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38602143/