Recursive XAML binding data templates on the Universal Windows Platform

我的梦境 提交于 2019-12-31 04:22:06

问题


So I have a class Task which has a couple of properties and also can have a list Task objects ( child tasks ) inside it. I would like to recursively display each Tasks and their sub-tasks on the 'UWP'. Apparently 'WPF' had special 'UserControls' for that purpose according to this post: Are recursive DataTemplates possible?

but they don't seem to be available on 'UWP'.


回答1:


You can use TreeView Control.

Here are the links to this project: NuGet:WinRTXamlToolkit and GitHub:WinRTXamlToolkit.

Install it in your project using Package Manager Console:Install-Package WinRTXamlToolkit.UWP

I've made a basic demo from your requirement. See codes below:

MainPage.xaml:

<Page
x:Class="TreeViewSample.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:TreeViewSample"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:controls="using:WinRTXamlToolkit.Controls"
xmlns:data="using:WinRTXamlToolkit.Controls.Data"
mc:Ignorable="d">

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <controls:TreeView  ItemsSource="{x:Bind myList}">
        <controls:TreeView.ItemTemplate>
            <DataTemplate>
                <data:DataTemplateExtensions.Hierarchy>
                    <data:HierarchicalDataTemplate ItemsSource="{Binding SubTasks}"></data:HierarchicalDataTemplate>
                </data:DataTemplateExtensions.Hierarchy>
                <TextBlock Text="{Binding Name}"></TextBlock>
            </DataTemplate>
        </controls:TreeView.ItemTemplate>
    </controls:TreeView>
</Grid>

MainPage.xaml.cs:

namespace TreeViewSample
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        public List<Task> myList;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            myList = new List<Task>();
            var  taskA = new Task("TaskA");
            taskA.SubTasks = new List<Task> {
            new Task("SubTaskA"),
            new Task("SubTaskB"),
            new Task("SubTaskC")
        };
        taskA.SubTasks[0].SubTasks = new List<Task> {
            new Task("SubSubTaskA"),
            new Task("SubSubTaskB"),
            new Task("SubSubTaskC"),
            new Task("SubSubTaskD")
        };
        myList.Add(taskA);
    }
}

    public class Task
    {
        public Task(string name) {
            this.Name = name;
        }

        private String name;

        public String Name
        {
            get { return name; }
            set { name = value; }
        }

        public List<Task> SubTasks { get; set; }

    }
}


来源:https://stackoverflow.com/questions/36711859/recursive-xaml-binding-data-templates-on-the-universal-windows-platform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!