Reputation: 1307
I am new to WPF and I am stuck in creating a Treeview that is bound to a list of lists of list, etc, the number of childnodes can increase as needed. I have created two HierarchicalDataTemplates to test off the code, but the child nodes are not appearing
My tree view is defined as
<telerik:RadTabItem Header="Lookup Sets">
<telerik:RadTreeView IsLoadOnDemandEnabled="True" ItemsSource="{Binding AttributeLookupSetConversions}">
<telerik:RadTreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type cm:AttributeLookupSetConversion}">
<CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerikDocking:RadSplitContainer}}, Path=DataContext.UpdateSelectionCommand}"/>
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="{x:Type cm:AttributeConversion}">
<CheckBox Content="{Binding Path=Name}" IsChecked="{Binding Path=IsSelected, Mode=TwoWay}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" Command="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type telerikDocking:RadSplitContainer}}, Path=DataContext.UpdateSelectionCommand}"/>
</HierarchicalDataTemplate>
</telerik:RadTreeView.Resources>
</telerik:RadTreeView>
</telerik:RadTabItem>
Is there something I am missing or will I have to create the nodes in code? I have a tabControl that has treeViews of lists of lists, the problem is one tab can contain items of another treeView that are linked relationally by an Id, so if I have say fro example
stundents -Student1 -Course 1 -Course 2 - Department 1 -Student 2 -Course 6 Department12 Course -Qualification 1 -WorkType
Upvotes: 0
Views: 1058
Reputation: 3629
Create a class as the root of all your ltems, which has a DisplayName
and an observablecollection of other Item
s:
public class Item : INotifyPropertyChanged
{
string _displayText;
public string DisplayText { get { return _displayText; } set { _displayText = value; RaisePropertyChanged("DisplayText"); } }
ObservableCollection<Item> _items;
public ObservableCollection<Model> Items { get { return _items; } set { _items = value; RaisePropertyChanged("Items"); } }
public event PropertyChangedEventHandler PropertyChanged;
internal void RaisePropertyChanged(string propname)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
}
}
Any other type (Student, Teacher, Course, Department, etc.) must be derived from this class. They might have their specific properties too.
public class Student : Item
{
}
public class Course : Item
{
}
public class Qualification : Item
{
}
Note that if you have two types of Course
class (with different Inner list of items), create two separate classes for them.
Now, you should populate the observableCollection in the view model propertly, and everything will be taken care of in a TreeView such as the following:
<TreeView DataContext="{Binding}" ItemsSource="{Binding Items}">
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Items}">
<TextBlock Text="{Binding DisplayText}"/>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
</TreeView>
Upvotes: 2