C# 使用特定的XML结构填充wpf TreeView
我四处搜索,但似乎找不到答案。我是WPF的新手,我真的对xaml部分感到困惑。那么,如何从下面创建类似xml的结构呢 我正在尝试为此xml构建一个视图:C# 使用特定的XML结构填充wpf TreeView,c#,xml,wpf,treeview,C#,Xml,Wpf,Treeview,我四处搜索,但似乎找不到答案。我是WPF的新手,我真的对xaml部分感到困惑。那么,如何从下面创建类似xml的结构呢 我正在尝试为此xml构建一个视图: <?xml version="1.0" encoding="utf-8"?> <Patients> <Patient> <ID>44</ID> <Name>Ben Garsia</Name> <Year>1985</
<?xml version="1.0" encoding="utf-8"?>
<Patients>
<Patient>
<ID>44</ID>
<Name>Ben Garsia</Name>
<Year>1985</Year>
</Patient>
<Patient>
<ID>22</ID>
<Name>Melisa Mayer</Name>
<Year>1968</Year>
</Patient>
<Patient>
<ID>33</ID>
<Name>Morgan Smith</Name>
<Year>1979</Year>
</Patient>
</Patients>
这是我的xaml:
<Window x:Class="LoadTreeView.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:LoadTreeView"
Title="MainWindow" Height="450" Width="800">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<TreeView Name="treeViewT">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:PatientsList}" ItemsSource="{Binding Patients}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="Patients" />
</StackPanel>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type local:Patient}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding ID}" />
<TextBlock Text="{Binding Name}" />
<TextBlock Text="{Binding Year}" />
</StackPanel>
</DataTemplate>
</TreeView.Resources>
</TreeView>
<StackPanel Orientation="Horizontal" Grid.Row="1" HorizontalAlignment="Center">
<Button x:Name="btnLoad" Content="Load file" Width="100" Click="button_Click"
HorizontalAlignment="Left" Margin="4" VerticalAlignment="Top"/>
</StackPanel>
</Grid>
在xaml的代码隐藏中,我这样填充它:
var patientsList = new List<PatientsList>();
patientsList.Add(patients);
treeViewT.ItemsSource = patientsList;
var patientsList=newlist();
患者列表。添加(患者);
treeViewT.ItemsSource=患者列表;
您将患者的数据模板定义为一个包含三个文本块的stackpanel,因此树中的每个节点都显示了这一点
相反,DataTemplate应该是您希望节点显示的内容,可能只是名称的单个文本块,可能带有图标
要为其他属性添加额外的节点,patient对象需要有一个对象集合(理想情况下是ObservableCollection),该集合可以是每个patient节点的ItemSource
一般来说,树状视图应该为顶级节点(在您的示例中为PatientList)提供ItemSource,并为患者定义HierarchycalDataTemplate,该模板将具有指向其子节点列表的ItemSource。然后,您将为该列表中的每种类型的对象定义另一个HierarchyCalDataTemplate(或仅定义一个DataTemplate)
在您的情况下,您可能需要一个PatientViewModel,它将为其属性公开字符串的ObservableList。您可以为树绑定创建一个单独的类(TreeViewModel)。 下面是与XML完全相同的树的代码 XAML:
视图模型:
public class ViewModel
{
public ViewModel()
{
Tree = new ObservableCollection<TreeViewModel>();
LoadData();
}
private void LoadData()
{
var list1 = new PatientsList();
list1.Patients.Add(new Patient { ID = 44, Name = "Ben Garsia", Year = 1985 });
list1.Patients.Add(new Patient { ID = 22, Name = "Melisa Mayer", Year = 1968 });
list1.Patients.Add(new Patient { ID = 33, Name = "Morgan Smith", Year = 1979 });
var root = new TreeViewModel();
root.Add(list1);
Tree.Add(root);
}
public ObservableCollection<TreeViewModel> Tree { get; set; }
}
公共类视图模型
{
公共视图模型()
{
Tree=新的ObservableCollection();
LoadData();
}
私有void LoadData()
{
var list1=new PatientsList();
列表1.Patients.Add(新患者{ID=44,Name=“Ben Garsia”,年份=1985});
列表1.Patients.Add(新患者{ID=22,Name=“Melisa Mayer”,Year=1968});
列表1.Patients.Add(新患者{ID=33,Name=“Morgan Smith”,年份=1979});
var root=new TreeViewModel();
root.Add(列表1);
添加(根);
}
公共可观测集合树{get;set;}
}
TreeView模型类:
public class TreeViewModel
{
public TreeViewModel()
{
Children = new ObservableCollection<TreeViewModel>();
Title = "Patients"; // Root node name
}
public string Title { get; private set; }
public ObservableCollection<TreeViewModel> Children { get; }
public void Add(PatientsList list)
{
foreach (var i in list.Patients)
{
var child = new TreeViewModel();
child.Title = "Patient"; // Level2 node name
child.Add(i);
Children.Add(child);
}
}
private void Add(Patient patient)
{
Add($"ID: {patient.ID}");
Add($"Name: {patient.Name}");
Add($"Year: {patient.Year}");
}
private void Add(string title)
{
Children.Add(new TreeViewModel { Title = title });
}
}
公共类树视图模型
{
公共树视图模型()
{
Children=新的ObservableCollection();
Title=“Patients”;//根节点名称
}
公共字符串标题{get;private set;}
公共可观察集合子项{get;}
公共作废添加(患者列表)
{
foreach(患者列表中的var i)
{
var child=new TreeViewModel();
child.Title=“Patient”//Level2节点名称
儿童。添加(i);
添加(child);
}
}
私人无效添加(患者)
{
添加($“ID:{patient.ID}”);
添加($“名称:{patient.Name}”);
加上($“年:{patient.Year}”);
}
私有无效添加(字符串标题)
{
Add(新的TreeViewModel{Title=Title});
}
}
<TreeView Name="treeViewT" ItemsSource="{Binding Tree}" >
<TreeView.ItemTemplate>
<HierarchicalDataTemplate DataType="{x:Type local:TreeViewModel}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Title}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
public class ViewModel
{
public ViewModel()
{
Tree = new ObservableCollection<TreeViewModel>();
LoadData();
}
private void LoadData()
{
var list1 = new PatientsList();
list1.Patients.Add(new Patient { ID = 44, Name = "Ben Garsia", Year = 1985 });
list1.Patients.Add(new Patient { ID = 22, Name = "Melisa Mayer", Year = 1968 });
list1.Patients.Add(new Patient { ID = 33, Name = "Morgan Smith", Year = 1979 });
var root = new TreeViewModel();
root.Add(list1);
Tree.Add(root);
}
public ObservableCollection<TreeViewModel> Tree { get; set; }
}
public class TreeViewModel
{
public TreeViewModel()
{
Children = new ObservableCollection<TreeViewModel>();
Title = "Patients"; // Root node name
}
public string Title { get; private set; }
public ObservableCollection<TreeViewModel> Children { get; }
public void Add(PatientsList list)
{
foreach (var i in list.Patients)
{
var child = new TreeViewModel();
child.Title = "Patient"; // Level2 node name
child.Add(i);
Children.Add(child);
}
}
private void Add(Patient patient)
{
Add($"ID: {patient.ID}");
Add($"Name: {patient.Name}");
Add($"Year: {patient.Year}");
}
private void Add(string title)
{
Children.Add(new TreeViewModel { Title = title });
}
}