Using C# and WPF I have a situation where my data is organized hierarchically.
I am using a treeview to represent this :
- Group_01
- Object_01
- Material_01
- Texture
- Material_02
- Texture_02
- Material_01
- Object_01
- Group_02
- Object_02
- Material_03
- Texture_03
- Material_03
- Object_02
I would like to be able to switch the order of the hierarchy, for example be able to create the data starting from the Materials and know all the objects that use that particular material
- Material_02
- Object_01
- Group_01
- Object_02
- Group_02
- Object_01
What is it the recommended data structure to organize the data? And how I could switch easily between any of these visualizations?
5
To be able to do this, you will need to modify your data type classes so that the Material
class has a collection of type Object
and your Object
class (which is a really bad name if this is a custom class) should have a collection of type Group
. The Object
class will also need a collection of type Material
and the Material
class will also need a collection of type Texture
, etc.
Then you would need to design a function (with loads of Linq
or loops) that reverses the hierarchy. The easiest way to do this is if you add properties into your data type classes that relate to each other. Therefore, they should all have some kind of Id
property and then the relevant child classes would need to have ParentId
properties.
With this setup, you can easily build your hierarchies:
Material material = new Material();
material.Id = theIdValue;
material.Textures = new ObservableCollection(allTextTures.Where(t => t.ParentId ==
material.Id).ToList());
1
Since here you have many to many relationships i.e group can have object list which inturn can have material list and the list of groups it belongs to… similarly material can have object list.. so your models should be like below:
public class Group
{
ObservableCollection<Object> Objects { get; set; }
}
public class Object
{
ObservableCollection<Material> Materials { get; set; }
ObservableCollection<Group> Groups { get; set; }
}
public class Material
{
ObservableCollection<Object> Objects { get; set; }
}
Now in order to show the hierarchical data you can use the TreeView to represent this and can define the HierarchicalDataTemplates depending on your number of visualization e.g for GroupHierarchy and MateraialHierarchy you can have templates like:
Group Hierarchy:
<HierarchicalDataTemplate x:Key="GroupTemplate" ItemsSource="{Binding Objects}">
<StackPanel>
<TextBlock Text="{Binding GroupName}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Materials}">
<TextBlock Text="{Binding ObjectName}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding MaterialName}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
Material hierarchy:
<HierarchicalDataTemplate x:Key="MaterialTemplate" ItemsSource="{Binding Objects}">
<StackPanel>
<TextBlock Text="{Binding MaterialName}"/>
</StackPanel>
<HierarchicalDataTemplate.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Groups}">
<TextBlock Text="{Binding ObjectName}"/>
<HierarchicalDataTemplate.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding GroupName}"/>
</DataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
</HierarchicalDataTemplate.ItemTemplate>
</HierarchicalDataTemplate>
Now you can use ItemTemplateSelector
to return the correct template for your TreeView
when you switch the TreeView’s ItemsSource
1
I would focus on what each object is without focussing on how it will be displayed. What is a node? Looking at your treeview, it looks like a node is an item with various properties – would a filtering system based on numerous categories perhaps work better than a treeview?
Then a user could say I want “material x” with “texture y” but only in “group b” without needing to navigate through a hierachy?
It’s hard to give you concrete advice without knowing what a node actually represents. Also, might be worth looking at ux.stackexchange.com for front end visualisation ideas.
3