c# - ListView 中的空间超出了我的需要

标签 c# .net listview xamarin xamarin.forms

我正在使用 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/

相关文章:

Android:如何从 XML 文件中的数据创建 CursorAdapter

c# - Url.Content 和读取 ~ 来自 xml 文件的 URL 中的代字号

c# - 单击 ComboBox 的 TextBox 部分

c# - C#/.Net Framework 中的堆大小 - 它可以增长吗?如何增长?

c# - 通过单击激活表单时防止表单中的焦点更改

c# - 如何使用字符串数组值对可序列化字典进行(反)序列化?

android - Android RecyclerView 中的一个按钮单击会触发另一个按钮

c# - WPF:如何将 Listview 列拆分为子列

c# - 子类型和协变返回类型

c# - 如何将实现某个通用接口(interface)的所有类型放入字典中?