【问题标题】:Multilevel Listview in xamarin formsxamarin 表单中的多级列表视图
【发布时间】:2021-08-06 02:16:35
【问题描述】:

我需要在 Xamarin 表单中创建多级列表视图。

目前,我在菜单页中将其用于单级分组

http://www.compliancestudio.io/blog/xamarin-forms-expandable-listview

我需要更新附件图片之类的内容。我已经尝试了一些解决方案,但都是徒劳的。

任何人都对此有任何想法。

谢谢

【问题讨论】:

  • 您可以尝试使用第三方树视图控件,但通常尝试使用 ListView 执行此操作是一个坏主意。就个人而言,如果我不得不做这样的事情,我会在深入每个小节时导航到一个新的列表视图
  • 你能不能放一些光,会很棒。
  • 问题是我们正在使用 Devexpress xamarin 表单控件,而他们没有任何此类控件。我们暂时不能使用任何其他第三方控制。
  • 不要尝试将 ListView 嵌套在 ListView 中。相反,您需要实现一个树视图(不幸的是,Xamarin 中没有内置树视图)。令人惊讶的是,我没有看到任何开源的。对于树是静态的菜单,您所需要的只是一种“展开”和“折叠”节点的方法。 This answer using StackLayouts might get you started.

标签: xamarin.forms expandablelistview


【解决方案1】:

您可以使用 ExpanderXamarin Community Toolkit 来做到这一点。

从 NuGet 安装 Xamarin Community Toolkithttps://www.nuget.org/packages/Xamarin.CommunityToolkit/

用法:xmlns:xct="http://xamarin.com/schemas/2020/toolkit"

Xaml:

<ContentPage.BindingContext>
    <viewmodels:RootViewModel />
</ContentPage.BindingContext>
<ContentPage.Content>
    <ScrollView Margin="20">
        <StackLayout BindableLayout.ItemsSource="{Binding roots}">
            <BindableLayout.ItemTemplate>
                <DataTemplate>
                    <xct:Expander>
                        <xct:Expander.Header>
                            <Label Text="{Binding Root}"
                               FontAttributes="Bold"
                               FontSize="Large" />
                        </xct:Expander.Header>

                        <StackLayout BindableLayout.ItemsSource="{Binding Node}">
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>                                        
                                    <xct:Expander Padding="10">
                                        <xct:Expander.Header>
                                            <Label Text="{Binding Key.Node}" FontSize="Medium" />
                                        </xct:Expander.Header>
                                        <StackLayout BindableLayout.ItemsSource="{Binding Value}">
                                            <BindableLayout.ItemTemplate>
                                                <DataTemplate>
                                                    <Label Text="{Binding SubRoot}"  FontSize="Small" />
                                                </DataTemplate>
                                            </BindableLayout.ItemTemplate>
                                        </StackLayout>

                                    </xct:Expander>
                                </DataTemplate>
                            </BindableLayout.ItemTemplate>
                        </StackLayout>
                    </xct:Expander>
                </DataTemplate>
            </BindableLayout.ItemTemplate>
        </StackLayout>
    </ScrollView>
</ContentPage.Content>

型号:

 public class Roots
{
    public string Root { get; set; }

    public Dictionary<Nodes, List<SubRoots>> Node { get; set; }        

}
public class Nodes
{
    public string Node { get; set; }
}
public class SubRoots
{
    public string SubRoot { get; set; }
}

视图模型:

 public class RootViewModel
{
    public List<Roots> roots { get; private set; }
    public RootViewModel()
    {
        CreateMonkeyCollection();
    }
    public void CreateMonkeyCollection()
    {
      
        List<SubRoots> subRoot = new List<SubRoots>() { new SubRoots() { SubRoot = "SubNode" } };
        Dictionary<Nodes, List<SubRoots>> node = new Dictionary<Nodes, List<SubRoots>>();
        node.Add(new Nodes() {  Node="Nodo1"}, null);
        node.Add(new Nodes() {  Node="Nodo2"}, null);
        node.Add(new Nodes() {  Node="Nodo3"}, null);
        node.Add(new Nodes() {  Node="Nodo4"}, subRoot);

        roots = new List<Roots>()
        {
            new Roots(){ Root="Root1", Node=node},
            new Roots(){ Root="Root2", Node= null}

        };

    }
}

【讨论】:

  • 谢谢,温迪。我能够创造我想要的东西。这是一个非常好的解决方案。但是,我尝试做的一件事是一次打开一个扩展器。我尝试了一些事情,但再次无法实现我想要的。此外,在站点中寻找一些,但主要是针对 WPF。知道我们如何在您的解决方案中做。
  • Bing IsExpanded 属性来控制。检查下面的链接。 stackoverflow.com/questions/67630688/…
  • 谢谢,温迪,我忘了发帖我能做到这一点。感谢您的帮助。
  • Wendy 很抱歉再次给您带来麻烦。任何帮助我可以在此列表中为我的选定项目设置背景颜色,就像 ListViewItem 一样。我已经完成了,但我对代码不是很满意,因为我遍历了许多父母和孩子,然后我设置了抽头扩展器和 Stacklayout 背景,并重置了之前选择的扩展器,我发现这不是一个好主意。如果有任何简单的方法可以做到这一点,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2020-06-03
  • 2019-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-17
  • 1970-01-01
相关资源
最近更新 更多