C# WPF TreeView 绑定复杂对象与内嵌控件

xaml:

复制代码
<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        d:DataContext="{d:DesignInstance local:ViewModel}"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">

    <Grid>
        <TreeView ItemsSource="{Binding Plans}">
            <TreeView.Resources>
                <HierarchicalDataTemplate DataType="{x:Type local:Model+Plan}"  ItemsSource="{Binding Project}">
                    <TextBlock Text="{Binding Name}"/>
                </HierarchicalDataTemplate>
                <DataTemplate DataType="{x:Type local:Model+Project}">
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto"/>
                            <RowDefinition Height="*"/>
                        </Grid.RowDefinitions>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock  Text="{Binding Name}" Margin="2"/>
                            <TextBlock Text="{Binding PassCount}" Background="green" Width="10" Margin="2"/>
                            <TextBlock Text="{Binding FailCount}" Background="red" Width="10" Margin="2"/>
                        </StackPanel>
                        <DataGrid Grid.Row="1" ItemsSource="{Binding TestSequence}"/>
                    </Grid>
                </DataTemplate>
            </TreeView.Resources>
        </TreeView>
    </Grid>
</Window>

cs:

复制代码
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        ViewModel vm = new ViewModel();
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = vm;
        }
    }
    public class NotifyPropertyChanged : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public class ViewModel : NotifyPropertyChanged
    {
        private ObservableCollection<Model.Plan> _plans = new ObservableCollection<Model.Plan>();

        public ObservableCollection<Model.Plan> Plans
        {
            get { return _plans; }
            set
            {
                _plans = value;
                OnPropertyChanged(nameof(Plans));
            }
        }

        public ViewModel()
        {
        }


    }
    public class Model
    {
        public class Plan : NotifyPropertyChanged
        {
            public String Name { get; set; }
            public ObservableCollection<Project> Project { get; set; } = new ObservableCollection<Project>();
        }

        public class Project : NotifyPropertyChanged
        {
            public String Name { get; set; }
            public string FailCount { get; set; } = "0";
            public string PassCount { get; set; } = "0";
            public ObservableCollection<TestSequence> TestSequence { get; set; } = new ObservableCollection<TestSequence>();
        }

        public class TestSequence : NotifyPropertyChanged
        {
            public bool Testcase { get; set; }
            public string Name { get; set; }
            public string Verdict { get; set; }
            public string Type { get; set; }
            public string Address { get; set; }
            public string Parameter1 { get; set; }
        }
    }
}

效果大概是这样: