对于我的treeview,我有两个不同的类提供ItemsSource。
public class TreeViewModel : ViewModelBase
{
public ObservableCollection<NodeViewModel> Items { get; set; }
}
public class NodeViewModel : ViewModelBase
{
public string Id { get; set; }
public string Name { get; set; }
public ObservableCollection<NodeViewModel> Children { get; set; }
}
现在我希望我的TreeView在TreeViewModel中显示Items并显示NodeViewModel提供的分层数据。
这是我的XAML
<Window x:Class="TreeViewMasterDetails.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TreeViewMasterDetails"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TreeView Height="Auto"
HorizontalAlignment="Stretch"
Margin="10"
VerticalAlignment="Stretch"
Width="Auto">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="x:Type local:TreeViewModel" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Path=Name}"></TextBlock>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="x:Type local:NodeViewModel" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Grid>
</Window>
尝试将Items
作为ItemsSource
的{{1}}。
如果显示任何内容,它不会按层次结构显示数据。
我尝试使用TreeView
代替ItemTemplate
。
它出了什么问题?
也许问题是第一个TreeView.Resources
?
我想在TextBlock Text Binding
中显示Name
的{{1}}属性。
答案 0 :(得分:23)
您只需为HierarchicalDataTemplate
声明NodeViewModel
,因为这是TreeView
中显示的唯一内容,并将实际ItemSource
绑定到TreeView
}}
<TreeView ItemsSource="{Binding Items}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
完整示例
的Xaml:
<Window x:Class="WpfApplication13.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication13"
Title="MainWindow" x:Name="UI" Width="343" Height="744.625" >
<TreeView DataContext="{Binding ElementName=UI, Path=TreeModel}" ItemsSource="{Binding Items}">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type local:NodeViewModel}" ItemsSource="{Binding Children}">
<TextBlock Text="{Binding Name}"></TextBlock>
</HierarchicalDataTemplate>
</TreeView.Resources>
</TreeView>
</Window>
代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
public TreeViewModel TreeModel
{
get
{
return new TreeViewModel
{
Items = new ObservableCollection<NodeViewModel>{
new NodeViewModel { Name = "Root", Children = new ObservableCollection<NodeViewModel> {
new NodeViewModel { Name = "Level1" , Children = new ObservableCollection<NodeViewModel>{
new NodeViewModel{ Name = "Level2"}}} } }}
};
}
}
}
public class TreeViewModel
{
public ObservableCollection<NodeViewModel> Items { get; set; }
}
public class NodeViewModel
{
public string Id { get; set; }
public string Name { get; set; }
public ObservableCollection<NodeViewModel> Children { get; set; }
}
结果:
答案 1 :(得分:3)
正如@ sa_ddam213所说,HierarchicalDataTemplate
只需要NodeViewModel
,但代码的唯一问题是{{
和}
缺少大括号{数据模板定义中的{1}}(应为DataType="x:Type local:TreeViewModel"
)。添加括号和DataType="{x:Type local:TreeViewModel}"
绑定可以解决问题:
ItemsSource
的额外HierarchicalDataTemplate
未使用,但不会造成伤害。