【问题标题】:Expandable ListView with different child items in Xamarin formsXamarin 表单中具有不同子项的可扩展 ListView
【发布时间】:2019-03-04 03:42:03
【问题描述】:

关于如何在 Xamarin 表单中实现具有不同子视图的可扩展列表视图的任何建议。有人可以帮我吗?

【问题讨论】:

  • 把你所有的内容放在一个StatckLayout中,你可以利用StackLayout的IsVisible属性来显示或隐藏Tapped时的StackLayout。需要更多说明吗?
  • @OluwasayoBabalola 是的,请
  • 我已经写了一个答案。如果有帮助,请告诉我。

标签: xamarin xamarin.forms expandablelistview listviewitem


【解决方案1】:

要为不同的单元格使用不同的模板,您需要使用 DataTemplateSelector,此处记录了它:Creating a Xamarin.Forms DataTemplateSelector

首先在单独的类中定义它:

public class PersonDataTemplateSelector : DataTemplateSelector
{
  public DataTemplate ValidTemplate { get; set; }
  public DataTemplate InvalidTemplate { get; set; }

  protected override DataTemplate OnSelectTemplate (object item, BindableObject container)
  {
    return ((Person)item).DateOfBirth.Year >= 1980 ? ValidTemplate : InvalidTemplate;
  }
}

然后将其添加到您页面的资源中:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" xmlns:local="clr-namespace:Selector;assembly=Selector" x:Class="Selector.HomePage">
    <ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="validPersonTemplate">
                <ViewCell>
                   ...
                </ViewCell>
            </DataTemplate>
            <DataTemplate x:Key="invalidPersonTemplate">
                <ViewCell>
                   ...
                </ViewCell>
            </DataTemplate>
            <local:PersonDataTemplateSelector x:Key="personDataTemplateSelector"
                ValidTemplate="{StaticResource validPersonTemplate}"
                InvalidTemplate="{StaticResource invalidPersonTemplate}" />
        </ResourceDictionary>
    </ContentPage.Resources>
  ...
</ContentPage>

然后在你的列表中使用它:

<ListView x:Name="listView" ItemTemplate="{StaticResource personDataTemplateSelector}" />

要能够展开/隐藏单元格,您需要:

  • 为特定列表项的 ViewModel 添加属性 IsExpanded
  • 在列表的 ItemSelected 事件上将其更改为 true/false
  • 将要隐藏/展开的视图的可见性绑定到 IsExpanded 的值

【讨论】:

  • 不要犹豫,询问您是否对这些模板有一些问题,如果它解决了您的问题,请记住将答案标记为已接受:)
【解决方案2】:

XAML

   <ListView ItemsSource="{Binding YOUR_SOURCE}" SeparatorVisibility="Default" 
             HasUnevenRows="True" ItemSelected="MyList_ItemSelected"> 
      <ListView.ItemTemplate>
          <DataTemplate>
             <ViewCell>
                <Frame>
                   <StackLayout>
                      <Label Text="My Heading"/>
                   </StackLayout>

                   <StackLayout x:Name="moreItemStack" Orientation="Horizontal" 
                                IsVisible="false">
                       <Label Text="child 1"/>
                       <Label Text="child 2"/>
                   </StackLayout>
               </Frame>
            </ViewCell>
         </DataTemplate>
      </ListView.ItemTemplate>
    <ListView>

C#

  private void MyList_ItemSelected(object sender, SelectedItemChangedEventArgs e)
  {
      var myItem = e.SelectedItem; 
      moreItemStack.IsVisible = true;          
  }

@user3932639 好了

注意:这是为了澄清而写的,它已经过测试。

【讨论】:

    猜你喜欢
    • 2015-08-15
    • 1970-01-01
    • 2015-10-16
    • 1970-01-01
    • 2019-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-07
    相关资源
    最近更新 更多