Recursive XAML binding data templates on the Universal Windows Platform

后端 未结 1 934
情话喂你
情话喂你 2021-01-25 15:57

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

相关标签:
1条回答
  • 2021-01-25 16:06

    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; }
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题