我一直在浏览教程并在线阅读一些关于树视图中多种类型的文章。但是,我没有遇到过一个教程,它解释了如何构建或处理包含嵌套的多个不同节点类型的树视图。例如,想一下典型的文件夹结构。文件夹可以包含文件和文件夹,其中的文件夹可以包含更多文件和文件夹。因此,我试图将其分解为一个简单的解决方案,希望有人可以修改/向我解释如何让一个家庭嵌套在另一个家庭中,如下图所示。到另一个堆栈溢出的链接并没有解释INode接口如何工作的详细信息。
据我所知,我知道我需要三节课。
第1课:FamilyNode
第2课:FamilyMember
第3课:INode
public interface INode
{
string Name { get; }
string Path { get; }
}
以下是“当前”下图中所示内容的代码。我知道它没有以最好的方式组织起来,我只是想得到一个简单的例子和解释。我希望你们可以提供帮助,为此研究耗费时间。我可以围绕如何创建可以是Family或FamilyMember类型的节点集合来开始构建树视图。谢谢你的帮助。
ViewModel.cs
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApplication1
{
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<Family> families;
public ObservableCollection<Family> Families
{
get { return families; }
set
{
families = value;
NotifyPropertyChanged("Families");
}
}
public ViewModel()
{
// FAMILIES
Families = new ObservableCollection<Family>();
Family family1 = new Family() { Name = "The Doe's" };
family1.Members.Add(new FamilyMember() { Name = "John Doe", Age = 42 });
family1.Members.Add(new FamilyMember() { Name = "Jane Doe", Age = 39 });
family1.Members.Add(new FamilyMember() { Name = "Sammy Doe", Age = 13 });
Families.Add(family1);
Family family2 = new Family() { Name = "The Moe's" };
family2.Members.Add(new FamilyMember() { Name = "Mark Moe", Age = 31 });
family2.Members.Add(new FamilyMember() { Name = "Norma Moe", Age = 28 });
Families.Add(family2);
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public class Family
{
public Family()
{
this.Members = new ObservableCollection<FamilyMember>();
}
public string Name { get; set; }
public ObservableCollection<FamilyMember> Members { get; set; }
}
public class FamilyMember
{
public string Name { get; set; }
public int Age { get; set; }
}
}
MainWindow.Xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:self="clr-namespace:WpfApplication1"
Title="MainWindow" Height="350" Width="305"
WindowStartupLocation="CenterScreen">
<Window.DataContext>
<self:ViewModel/>
</Window.DataContext>
<Grid>
<TreeView Name="trvFamilies" ItemsSource="{Binding Families}" Grid.Row="1" Grid.ColumnSpan="2">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type self:Family}" ItemsSource="{Binding Members}">
<StackPanel Orientation="Horizontal">
<Label VerticalAlignment="Center" FontFamily="WingDings" Content="1"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type self:FamilyMember}">
<StackPanel Orientation="Horizontal">
<Label VerticalAlignment="Center" FontFamily="WingDings" Content="2"/>
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
答案 0 :(得分:2)
我找到了与朋友交谈的解决方案,并想到我会在这里发布。
<强> ViewModel.cs 强>
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace WpfApplication1
{
public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<INode> families;
public ObservableCollection<INode> Families
{
get { return families; }
set
{
families = value;
NotifyPropertyChanged("Families");
}
}
public ViewModel()
{
// FAMILIES
Families = new ObservableCollection<INode>();
Family family1 = new Family() { Name = "The Doe's" };
family1.Members.Add(new FamilyMember() { Name = "John Doe", Age = 42 });
family1.Members.Add(new FamilyMember() { Name = "Jane Doe", Age = 39 });
family1.Members.Add(new FamilyMember() { Name = "Sammy Doe", Age = 13 });
Families.Add(family1);
Family family2 = new Family() { Name = "The Moe's" };
family2.Members.Add(new FamilyMember() { Name = "Mark Moe", Age = 31 });
family2.Members.Add(new FamilyMember() { Name = "Norma Moe", Age = 28 });
Families.Add(family2);
Family family3 = new Family() { Name = "The Dunkin's" };
family3.Members.Add(new FamilyMember() { Name = "Kevin Dunkin", Age = 31 });
family3.Members.Add(new FamilyMember() { Name = "Breana Dunkin", Age = 28 });
family2.Members.Add(family3);
}
public event PropertyChangedEventHandler PropertyChanged;
public void NotifyPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
public interface INode
{
string Name { get; }
}
public class Family : INode
{
public Family()
{
this.Members = new ObservableCollection<INode>();
}
public string Name { get; set; }
public ObservableCollection<INode> Members { get; set; }
public Family Parent { get; private set; }
}
public class FamilyMember : INode
{
public string Name { get; set; }
public int Age { get; set; }
}
}