我需要在wpf中为树视图绑定递归子节点。但我真的不知道如何使它工作。这就是我找到的
ObservableCollection<Animal> animals = new ObservableCollection<Animal>();
animals.Add(new Animal("California Newt"));
animals.Add(new Animal("Tomato Frog"));
animals.Add(new Animal("Green Tree Frog"));
animals.Add(new Animal("Frog"));
AnimalCategories.Add(new AnimalCategory("Amphibians", animals));
animals = new ObservableCollection<Animal>();
animals.Add(new Animal("Golden Silk Spider"));
animals.Add(new Animal("Black Widow Spider"));
AnimalCategories.Add(new AnimalCategory("Spiders", animals))
需要在树视图上绑定的列表,这里是它的结构
AnimalCategory类:
Class AnimalCategory
Private _category As String
Public Property Category As String
Get
Return _category
End Get
Set(ByVal value As String)
_category = value
End Set
End Property
Private _animals As ObservableCollection(Of Animal)
Public ReadOnly Property Animals() As ObservableCollection(Of Animal)
Get
If _animals Is Nothing Then
_animals = New ObservableCollection(Of Animal)()
End If
Return _animals
End Get
End Property
Public Sub New()
End Sub
Public Sub New(category As String, animals As ObservableCollection(Of Animal))
_category = category
_animals = animals
End Sub
End Class
动物类:
Class Animal
Private _name As String
Public Property Name As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(name As String)
_name = name
End Sub
End Class
我的xaml代码是..!
<TreeView x:Name="radTreeView" Background="#4E4E4E" x:FieldModifier="public" ItemsSource="{x:Static local:cntlWPFMMTreeView.AnimalCategories}"
ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.VerticalScrollBarVisibility="Visible">
<telerik:RadTreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Animals}">
<TextBlock Text="{Binding Category}" />
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</telerik:RadTreeView.ItemTemplate>
</TreeView>
使用上面的代码我可以绑定一个级别的父子hiereachy,如下所示,
Buit我需要像下面的动态
那样绑定多层次的层次结构如何在WPF中执行此操作..?
答案 0 :(得分:0)
您可以将一个控件的值与另一个控件的datacontext绑定为 -
Binding =“{Binding Path = DataContext.MyBindingProperty,ElementName = MyMainWindow}
根据您的要求修改此行。
希望它有所帮助!
答案 1 :(得分:0)
两件事: 您必须向动物类添加一个ObservableCollections of Animals。 并且您必须使用ItemsSource =“{Binding Animals}”将HierarchicalDataTemplate的ItemTemplate更改为另一个HierarchicalDataTemplate。