Is there any way to create infinite children nodes in TreeView using DataTemplate?
I read lots of tutorials, but all of them show only manually populated TreeView. I need to populate my TreeView from database.
Try using HierarchicalDataTemplate; To populate data from DB you will have to first fetch the data, create proper DataModel objects and then use them to populate the TreeView. I would also suggest you to use MVVM for this, MVVM makes it very easy to work with TreeView.
Have a look at following great article from Josh Smith explaining both these (and much more)-
Simplifying the WPF TreeView by Using the ViewModel Pattern
Related
I am new to UWP and C# but I have been tasked with creating a UI that is created dynamically based on an XML file that is read in.
I am having trouble updating values that are nested inside an ObservableCollection.
The current structure uses StackPanels as a container to add UI elements to. The heirarchy looks like this when the elements are created.
StackPanel = Outermost container panel
-StackPanel = Section Stack panel :implements ObservableCollection
--StackPanel = Entry container panel :implements INotifyPropertyChanged
---StackPanel = Entry inner Wrapper panel :implements INotifyPropertyChanged
---- This is where the UI elements like textboxes live
---- TextBox
---- TextBox
I need extract the values of the TextBoxes but they don't ever update from their original value.
I am having trouble finding documentation that points to a good way of doing this kind of thing (usually means it shouldn't really be done haaa) any input would be much appreciated.
You need to do the following:
Read the data from your xml to your ObservableCollection to build a data as base data structure.
Use binding to bind it to your UI elements from your ObservableCollection. By the way, as Peter mentioned, make sure you've already made it twoway binding, in that way the changes from your UI will reflect the data store in memory
Use some ways(for example use a button) to save changes from your UI and then update and save the data in memory to your original xml file.
Some documention can be found from the following:
https://learn.microsoft.com/en-us/windows/uwp/data-binding/data-binding-quickstart
Code Sample:https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/XmlDocument
I ended up using this link to solve my problem.
http://www.teixeira-soft.com/bluescreen/2016/03/23/c-how-to-dinamicaly-select-a-datatemplate-for-each-item-in-a-list/
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.
My data comes from a database. I have an item data table with some basic properties.
Item
{
ID,
Name,
Description,
...
}
Then I have a many to many relationship table with:
Parent
{
ParentID,
ChildID
}
I'm iterating through each item and displaying its children; and its children's children, etc. I assume this would best be accomplished with nested repeaters, but I could be wrong.
How do I get multiple heirarchical levels using asp:repeaters? I've only ever used one nested repeater, I have no idea how to do 3+.
Personally I probably do this by creating a custom control with an Item property and some sort of either Parents or Children property. The control would display details on Item, and then use a repeater to show each element in Parents / Children, where for each item the repeater recursively uses the same control to render the item.
I would do what #Kragen said. But if you really think that creating two components is too much you should use one repeater and two foreach loops in on data bound.
Using two repeaters one inside of another is overcomplicated not to mention 3 repeaters :).
A Parent-Child relationship, which is many-to-many? This doesn't make sense, at least calling them a parent and a child doesn't make sense. If it is really many-to-many, it's a network, if a child only has ONE parent, then it is a hierarchy.
With the former, I'm not sure visualizing them in that way is any good. How do you visualize a child with more than one parent?
With the latter, why not use a TreeView with a custom item template? It handles all the hierarchy stuff automatically. The problem with repeaters is that if you are not doing them dynamically (creating them in code), then the level of nesting is fixed and you cannot go as much as you want. Doing them dynamically would work, but would bring overhead.
I'm not very sure what exactly you are trying to accomplish. Maybe providing some more details about the end-result you need to see would be helpful.
I've got an ObservableCollection of POCOs (Plain old CLR Objects) that I want to represent in a tabbed idiom. Prefereably using the MVVM pattern, is there a way to bind the collection of TabItems to the count of my POCO collection?
So, in this case if there are 3 items in my collection, I'd like to see 3 TabItems. Each TabItem would contain the same controls in the same location, each control bound to properties of the appropriate object in the collection.
I'm just looking for an overview of the approach I might use or a link to an example. If you need more info, feel free to ask.
Thanks.
I'd probably create an ObservableColletion with your POCO items in it. You could then bind that ObservableCollection to any of the Silverlight Item Rendering controls. You'll have to modify the default rendering template to create your tabs...but using that method, your tabs will constantly be up to date with the items in the collection without having to put any code in the code behind file.
UPDATE
Here's a link to the Silverlight Forums where somebody built a TabControl using the ItemsControl with sample XAML code:
http://silverlight.net/forums/t/12271.aspx
...just scroll down a bit to see the sample.
One way to do this is to use a value converter (IValueConverter) to return your POCO wrapped in a TabItem. I posted an example here as part of a related question. There is also sample xaml binding and injection of the ViewModel as a parameter to the value converter.
/jhd
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.