Implement WPF TreeView of the same structure as another TreeView - c#

I have a TreeView which data source is generated in runtime through code to which I don't have access. Its hierarchical data, nodes of tree with 2, 3 or 4 depth levels. I have to make the same structured tree with RadioButtons corresponding to every object in first tree. Is there a way to iterate through every element of TreeView or another method to do my task?

In WPF, we use DataTemplates, or in your case HierarchicalDataTemplates to define how our data should be presented. You already have the data in the first TreeView and that should be accessible from its ItemsSource or Items properties. Therefore, all you need to do is to define another HierarchicalDataTemplate to display the same data object the way that you want it. There is no need to manually iterate through all of the nodes from the original TreeView.

Related

How to create a binded copy of an object in c#?

I have an object of class Employee (let us call it Alice), and want to create a copy of this object (let us call it Alice_again) such that if I change Alice, the properties of Alice_again also changes. Basically creating a copy of an object whose properties are bound to the original.
Edits
I am having two diferent ObservableCollection of Employee(Employee is User Control) and both contains same object Employee and I want to display each ObservableCollection once. For that I am using ItemsControl and I have binded ItemsSource of ItemsControl to these ObservableCollection and as I have read on this link that a given object may only be present in a given logical tree once, so I am trying to create a copy of object but I don't want to update both copies whenever there is an update.
You do not need to create a copy.
Since the elements in the ItemsSource collection are not supposed to be Visuals, two or more source collections may contain references to the same objects.
Two different UI elements in the ItemTemplate of two ItemsControl would hence legitimately bind to the same data item object.
object may only be present in a given logical tree once
logical tree is a tree of UI elements. Elements of a different type are not included in this tree.
For each item in the source collection, its own ContentPresenter will be created, which will be included in the logical tree of the parent ItemsControl.
And each ItemsControl creates its own collection from the ContentPresenter.
Therefore, different ContentPresenters will be created in different ItemsControls for the same source item and this will not create any problems.
You can even include the same item multiple times in the same collection.
And for each position of this element, its own ContentPresenter will be created.
dataTemplate has buttons whose content is binded to properties of Employee
A data template is, in fact, a factory for creating UI elements.
And when applying the template for each element, its own unique UI elements will be created.

Show records belonging to treeview's selected last node

I have a treeview that displays a few nodes, say 5 or so. When the final node is selected, I want to display records belonging to that final node (linked via a foreign key) inside of a listbox.
So the structure would be
Treeview Listbox
-1 -Object belonging to 5
--2 -Object belonging to 5
---3
----4
-----5
My question is if such a comperation between these two controls is even possible and if I'm going about it the smartest way.
I can't find anything about it (getting actual data from the last selected node in the treeview is already pretty hard on how to find a how to). Any tips in the right direction would be very appreciated.
It is possible to do this exactly the way you're trying to by using attached properties, but it's a bit of a clumsy way of going about it. What you really should be doing is using data binding.
Your TreeView is, presumably, bound to some kind of data structure in your view model (data context), and what class that is should (again, presumably) be able to easily ascertain whether or not a particular given item is the deepest/last one in the tree. So create a property in that class and bind the TreeView's SelectedItem to that, so that it gets updated whenever the user selects an item in the Tree. Next, create another property for your ListBox to bind to. When your first property gets set, it either sets this second property to the currently selected item if it's the last in the list, or sets it to null if it isn't.
By doing this you de-couple your logic from your view and you make something that's much easier to debug, test and modify in future.

Custom Query Dynamic Data Application

I am trying to use a Dynamic Data Application and having issues with customizing a query. Everything scaffolds wonderfully, however, I want to be able color code the main Parent table when a Child Objects Property is a certain value. Normally I would just iterate over the data using LINQ however, all the information I've researched always filters and color codes items in the grid based on the Parent level data. Any ideas on how to do this on Child Data?
In the ItemDataBound event of the child control, if the condition is met, you can walk up the control hierarchy recursively until you reach the row/item of the parent grid. Once you get to the row/item, you can set the color to whatever you need.

Pattern / Methodology For Dynamic ContextMenu Based on Collection of Objects

Background
I have a TreeView that follows the MVVM design pattern and supports multiple selection by recording TreeViewItem selections in a List. As it stands there are several types of TreeViewItems available for the user to select.
They are:
Two Root nodes of type WorldFolder and MyDataFodler which can contain child Folder types
Child Folder nodes of types LocationFolder, PersonFolder, CollectionFolder
Child Item nodes of type LocationItem, PersonItem
CollectionFolder can contain child nodes of Folder types
In all this works very well with very little code and supports collections of Locations and People and furthermore Collections within Collections.
Problem / Question
My top level view-model keeps track of the selection state of TreeViewItems and the current selection may be a combination of Item, Folder or even Root type nodes. Depending on the user's selection I want to create a dynamic ContextMenu. So far this works! When I select several LocationItem and/or PersonItem type nodes my view-model generates a custom ContextMenu. The problem is the complexity! My view-model is quickly turning into dozens of if/else if/else statements to capture all the possible permutations!
For example:
if (_selectedItems.All(item => item is PersonItem)) // Only people selected
{
// Create ContextMenu based on only PersonItems
}
else if( _selectedItems.All(item => item is LocationItem)) // Only Locations
{
// Create ContextMenu based only on LocationItems
}
...
Is there a better way to handle all the possible permutations of user choices and generate my ContextMenus more efficiently?
* Sorry about the code formatting, it's been giving me grief all week *
I can't remember where I read this wise saying:
The best way to work with a TreeView is not not to work with the TreeView
What does this mean? Move the functionality into the tree nodes and keep the tree view as thumb as possible. Unfortunately, by default the tree node does not many events, though, it's easy to redirect the tree view events to the nodes.
Once done, you can override the ContextMenuStrip property in your nodes. The first selected node creates the list of ToolStripItems it wants to handle and asks the tree view which are allowed (e.g. with a FilterMenuItems(desiredItems) method). The tree view asks all selected nodes which of the nodes they would be able to handle. The result is your context menu.
This should work with almost any count of different nodes and keeps the tree (nodes) easy to maintain.
Edit: Dang! Missed the WPF tag, so I cannot asses the available events, since I did not yet work with WPF

Best way to bind node collection to itemscontrol in Silverilght

I have a Silverlight project where the main objects are just a bunch of nodes that are joined to each other. A parent node can have many children.
I want to be able to bind the nodes to an itemscontrol or similar and so was wondering how to best structure the parent child relationship.
Is it OK to create a flat top level list of all nodes (List allNodes) and add each node to that, binding the list to the itemscontrol, then on top of that add each node to it's parent's 'childnodes' list to establish the structure - or am I doing some kind of ugly doubling up?
Just hoping there is some kind of best practice or pattern I can latch on to
Thanks
You should use the treeview found in the official silverlight toolkit. Here is an online demo.

Categories