I am trying so set up a TreeView for a Group/User hierarchy, where a group can have users and subgroups, and subgroups also subgroups and users, and so on. When I add/remove a user or group from a collection and update the view with myTreeView.Items.Refresh();
all expanded TreeViewItems get closed. This is inconvenient for the user, so i am trying to expand all TreeViewItems which were expanded before. myTreeView.SelectedItem;
doesn't seem to work, it only returns a Group
or User
Element no TreeViewItem
Element.
Now, I found something here WPF DataBound treeview expand / collapse that I tried, but the compiler is telling me this now
BindingExpression path error: 'IsNodeExpanded' property not found on 'object' ''User'
A User can't be expanded, so implementing this Field in the User class wouldn't make sense. What am I doing wrong? Is this even the right approach?
I have following setup for my TreeView
<TreeView Grid.Column="0" Name="myTreeView" SelectedItemChanged="myTreeView_SelectedItemChanged" MouseDoubleClick="myTreeView_MouseDoubleClick">
<TreeView.Resources>
<HierarchicalDataTemplate DataType="{x:Type datatypes:Group}" ItemsSource="{Binding Items, UpdateSourceTrigger=PropertyChanged}">
<TextBlock Text="{Binding Name}" FontWeight="Bold"/>
</HierarchicalDataTemplate>
<DataTemplate DataType="{x:Type datatypes:User}">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding LicenceUser}"/>
</StackPanel>
</DataTemplate>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="{Binding IsNodeExpanded, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</Style>
</TreeView.Resources>
</TreeView>
For better visibility I only copy the interfaces for my classes here.
Here the interface for my Group class
interface IGroup {
int ID { get; set; }
string Name { get; set; }
string Gruppe { get; set; }
string Path { get; set; }
Users Users { get; set; }
ObservableCollection<object> Items { get; }
bool IsNodeExpanded { get; set; }
Groups SubGroups { get; set; }
}
the Items Collection in the Group class looks like this
public ObservableCollection<object> Items {
get {
ObservableCollection<object> childItems = new ObservableCollection<object>();
foreach (Group item in SubGroups) {
childItems.Add(item);
}
foreach (User item in Users) {
childItems.Add(item);
}
return childItems;
}
}
and the interface for my User class
interface IUser {
string UserID { get; set; }
string LicenceUser { get; set; }
string MailAddress { get; set; }
string ComputerName { get; set; }
string HardDriveID { get; set; }
string Group { get; set; }
string Path { get; set; }
}
Thanks in advance for your help.