0

My problem is the following: I got a Tree which has an dynamic depth of categories (each category can have sub-categories as well as entries).

Now I added a HierarchicalDataTemplate for the categories to display correctly in the TreeView. But I get a lot of empty entries, which do not apply the Template (wrong type) but show up in the tree as 'corpse'.

How can I ban them from the generation process? Because it's an abstract tree, they are of the same base-class as the categories are. So they get into the tree, because the tree always searches the "Branches"-property which contains either categories, entries or both.

Any ideas? I didn't find any event of the TreeView which probably give me the opportunity to skip various entries during generation nor any option/property of the template to do so.

Detailed Description: I got a generic Tree class. This class has branches of type "A_TreeLeaf" (abstract). The Tree's generic type must inherit A_TreeLeaf of course. My data is structured in categories (CategoryTreeLeaf) and Data (DataTreeLeaf). Each leaf can have sub-leaves (branches), of course.

Now I load my data from a database and build the tree. Each category has X sub-categories. And each category also could contain some Data. This structure helps me a lot, because I got an clear hierarchic structure of categories and data. This way it should be visualized to the user. But I want to separate Data and Categories. The TreeView should show just the categories (by an HierarchicalDataTemplate) and the ListView just the Data (by an DataTemplate). The ListView works fine, but the Tree shows some "corpse"-entries which are the DataTreeLeaf-instances.

I want to filter the DataTreeLeafs on generation or just stop the TreeView displaying them. Is there any "non-hack" solution? I don't want to copy the tree and remove the Data-leaves unless it's really necessary... because this would cause a lot of overhead work to do for me and to manage either the code behind which uses the real tree or the visualization with the fake-tree (because I need to bridge it somehow that it's updated automatically when one of both changes).

SharpShade
  • 1,761
  • 2
  • 32
  • 45

1 Answers1

2

You have a unique problem... you have some data items in your hierarchical data that you don't want to display, but for some reason can't remove. If that sums up your problem, then you're doing something wrong.

In WPF, you shouldn't need to hide data items from the UI, instead you simply don't put them into the collection in the first place. It sounds like your process of filling your hierarchical data is flawed and you'd be better off fixing that at the source than trying to deal with the problems that it causes in the UI.

If you can't fix the actual process for whatever reason, then your next best option is to iterate through the data before you display it and simply remove any data elements that shouldn't be there. When using WPF, it is always best to provide your UI with data that fits the purpose.

However, if for whatever reason you can't even do that, then your last option is to simply define an additional DataTemplate for your abstract base class and just leave it empty:

<DataTemplate DataType="{x:Type YourDataTypesPrefix:YourBaseClass}">
</DataTemplate>

Of course, you'd have to define DataTemplates for each sub type, or they'd also be rendered empty.

Sheridan
  • 68,826
  • 24
  • 143
  • 183
  • Isn't there any other way? The current advantage is, that my "Tree" has all items needed in the code behind to make everything working. The UI just shouldn't show the entries, because they are shown separately in a listview. If I now remove all entries, the code won't work, or with massive complications. If I copy the tree, remove the entries and then show the copied tree, they are not anymore bound together. If I change the tree, nothing changes in the TreeView. Of course I could copy it everytime, but that's a huge overhead ... (EDIT: The empty DataTemplate doesn't work :-/) – SharpShade Feb 15 '14 at 10:47
  • You can add a new property to your nodes (leaves), for example `VisibleData` to return only the nodes that should be displayed. Linq queries come here handy. In general, in MVVM approach you have your model, the objects that hold the data and you manipulate them. Upon them you create a "displayable version" of your models, called view model. The view model is suited for UI purposes, and -in your example- will expose only the data that gets displayed. – RoadBump Jan 22 '17 at 16:29