WPF 分层数据模板和树视图
WPF HierarchicalDataTemplate & Treeview
我正在尝试将递归集合绑定到 WPF 中的 Treeview。我只能在 UI
中看到第一层
public class Employee
{
public string Name { get; set; }
public List<Employee> Reportees { get; set; }
}
Employees = new ObservableCollection<Employee>
{
new Employee { Name = "Bob1", Reportees=new List<Employee>{ new Employee { Name = "Bob1's Sub1"}, new Employee { Name = "Bob1's Sub2"} } },
new Employee { Name = "Bob2", Reportees=new List<Employee>{ new Employee { Name = "Bob2's Sub1"}, new Employee { Name = "Bob2's Sub2"} } },
new Employee { Name = "Bob3", Reportees=new List<Employee>{ new Employee { Name = "Bob3's Sub1"}, new Employee { Name = "Bob3's Sub2"} } }
};
XAML
<TreeView HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Height="320" Width="300" ItemsSource="{Binding Employees}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:Employee}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type System:String}">
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
您的 HierarchicalDataTemplate 缺少 ItemsSource
像这样:
<TreeView HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Height="320" Width="300" ItemsSource="{Binding Employees}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:Employee}" ItemsSource="{Binding Reportees}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type System:String}" ItemsSource="{Binding Reportees}">
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
我正在尝试将递归集合绑定到 WPF 中的 Treeview。我只能在 UI
中看到第一层public class Employee
{
public string Name { get; set; }
public List<Employee> Reportees { get; set; }
}
Employees = new ObservableCollection<Employee>
{
new Employee { Name = "Bob1", Reportees=new List<Employee>{ new Employee { Name = "Bob1's Sub1"}, new Employee { Name = "Bob1's Sub2"} } },
new Employee { Name = "Bob2", Reportees=new List<Employee>{ new Employee { Name = "Bob2's Sub1"}, new Employee { Name = "Bob2's Sub2"} } },
new Employee { Name = "Bob3", Reportees=new List<Employee>{ new Employee { Name = "Bob3's Sub1"}, new Employee { Name = "Bob3's Sub2"} } }
};
XAML
<TreeView HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Height="320" Width="300" ItemsSource="{Binding Employees}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:Employee}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type System:String}">
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
您的 HierarchicalDataTemplate 缺少 ItemsSource
像这样:
<TreeView HorizontalAlignment="Left" Margin="10" VerticalAlignment="Top" Height="320" Width="300" ItemsSource="{Binding Employees}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type model:Employee}" ItemsSource="{Binding Reportees}">
<TextBlock Text="{Binding Name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type System:String}" ItemsSource="{Binding Reportees}">
<TextBlock Text="{Binding}" />
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>