Flattening out a TreeView in WPF - wpf

I have some hierarchical that I'd like to display in a TreeView, but formatted to look like a ListBox.
The data I have looks like this (with the possibility of any number of sub levels):
Item 1
Child 1
Child 2
Item 2
Child 3
Child 4
I'd like the data to be displayed like this (and wrap when need):
Item 1 Child 1 Child 2 Item 2 Child 3 Child 4
I'd like to use the TreeView so I can maintain the relationship between the parent and child items which is why I don't want to use a ListBox.
Thanks!

It sounds like the issue you have is that you don't have a clear separation between your data structure and your view. If you have a tree which is not WPF it should be easy to map a tree view onto it. If you have a tree, you can create an tree node numerator that can be used in a list view.
By doing this, the relationship never changes - just how the data is projected in the UI.

I think the best way would be to implement a recursive function that will scroll down your tree until it hit the end. That function would take a referenced list or collection as parameter and the current node.
here is the pseudo-code for it
Sub GenerateListFromTree(Node oNode, List oList)
AddItemToList(oNode.Name)
For each Node oChildNode in oNode.Nodes
GenerateListFromTree(oChildNode,oList)
Next
End Sub

Related

WPF TreeView ICollectionView remove filter

I have a WPF TreeView which varies in depth. My application also has the ability to search inside the tree. This is accomplished by using the Filter property of the ICollectionView interface. Searching and filtering all works fine, but the problem lies when I try to remove the filter.
I have the following scenario; I enter search criteria and the treeview is filtered and the result is shown. Now when I press my button to clear the filter, the filter is removed but the problem is that it shows only the child nodes of what I previously searched for and does not show the child nodes of the other nodes, these nodes are also not expandable anymore.
My search function recursively walks down the tree and tests each node for the entered search text.
Do I have to apply the view.Filter = null; statement for each node and their childnodes?
Thanks,
Grant
Yes!
Every hierarchy level has it's own ICollectionView and each uses it's own filtering. So yes, yu have to clear the filter on every (parent-)node (depending on your exact implementation).

WPF: Problem with Hierarchy

I've noticed a problem I'm having depending on the Hierarchy of items. When I wan't to represent the items I have no idea how to put it's children in a manageable state.
Let's take a TreeView for example. When the window is loaded up I can take the model and put it into a view and as a source for the tree. However I seem to have no real control over the children. Only thing I can do is point to the children property of object and it shows up all real nice but what if I wan't to edit, sort and/or add a new item there I'm out of luck.
I can't really put the children into the view or a seperate collection to modify as they are only the property of the root element. Only way I can see at the moment is having the property return a ObservableCollection or a CollectionView and that has it's own problems as in no lazy loading for example.
How are you doing this, what is the proper way here and what am I missing?

WPF: How to use views like ICollectionView and IEditableCollectionView

I understand the syntax but not how really to use it. It's clear in many basic scenarios but as soon as it get's a little bit advanced I start getting a headache.
For example there are many different views but often not clear wich one to use. Also should you use always just one or mix and match. Do you use the view as your itemssource for ItemsControls?
I'm gonna give a scenario. I have items from a database that I need to show info about in a app and also allow to edit and add new. The items form a hierarchy and the models are of different types. So the top level have children and they then have children.
I could show it in a TreeView or some itemscontrol. Problem here is I tend to bind to the children property of the root elements wich returns a List of chilren. Now the children aren't really inside a view, like I can't call editview.addnew() or filter the children straight away. Question is how do I ensure the children are also in a view and their children and so on. Should the model return a view, should I create a seperate view for each children type or even for each parent?
Another thing is if I'm allowing editing should I put the Collections straight into a IEditableCollectionView or wrap it in ICollectionView first (why is that better)?
Is there a good guide to using views that isn't just pure basics?

Winforms .net 2.0: binding a textbox to a parent property

I've got a strongly typed dset, w/two datatables: Parent and Child, w/a relation linking them in the way you'd expect (e.g. parent-has-many-children).
On my Child Detail form, I show lots of info on the current child, w/binding calls like so:
me.txtBirthDate.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Child.Birthdate"))
But I would also like to show some info on the child's parent--say, the parent's name. I have tried:
me.txtParentName.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Child.Parent.Name"))
and
me.txtParentName.DataBindings.add(New Windows.Forms.Binding("Text", MyDataset, "Parent.Name"))
But these both result in a blank text box.
I can of course put the parent properties directly on the Child DataTable & fill them w/the results of a join between the underlying db tables, but I'd like to avoid that if it's possible (my real app involves just a few Parents each w/many many Children & I'd like not to be moving so much unnecessary data).
Is that possible?
Many thanks!
-Roy
I usually use a BindingSource between the DataSet and the controls. When using this approach, if I have controls that I want to bind to a related row from a different table, I create a new BindingSource, which points at the related row (his DataSource property is the original BindingSource, his DataMember is the parent table), and then bind the other controls to the 2nd BindingSource.
I usually do it all in the designer view, which just creates those BindingSource objects as I go along.
Hope that helps.

WPF Control Puzzle

I am new to WPF and am wondering how to best achieve a master detail grid as shown below.
The user will be able to press the right/left arrow keys to open/close Parents or click on the icon to achieve the same result. The data structure will be a parent/child 1 level deep.
How would I go about this?
-------------------------------------
Parent1 (P1) ^
-------------------------------------
P1 - Child 1
-------------------------------------
P1 - Child 2
-------------------------------------
Parent2 (P2) ^
-------------------------------------
P2 - Child 2
-------------------------------------
Parent3 (P3) >
-------------------------------------
Thanks in advance...
A couple of possibilities:
Use a TreeView, with suitable HierarchicalDataTemplates for the parent and child levels. I believe this will handle the arrow keys for you, but you may have to do more extensive templating to align everything correctly (ItemContainerStyle and the TreeViewItem.Template property would be the starting point).
Use an ItemsControl, and have your ItemTemplate include an Expander. The Header of the Expander would show the parent. The content of the Expander would be another ItemsControl, bound to the child items and with its ItemTemplate set to the appropriate detail view. Again, you would probably need to template the Expander to put the "expand/collapse" icon on the right rather than its default position on the left. You'll need to handle the arrow keys yourself in this case I think. The advantage is that this will naturally give you "stack" alignment (accordion style) rather than indenting.

Resources