Deleting or adding a node in WPF MVVM Treeview - wpf

I am new to these stuffs. Hence I request for your help.I want to delete and add a node to a treeview in WPF MVVM. I have managed to create the treeview using :
http://www.codeproject.com/Articles/354853/WPF-Organization-Chart-Hierarchy-MVVM-Application
but now I am not able to figure out how can I add/delete a node and then refresh the treeview.
Help is appreciated.

That is the problem with copying other people's code and not bothering to find out how it works. In WPF, we manage data elements, not UI elements, so to add another item into any UI container control, we simply add a data item into whichever data collection that is data bound to the ItemsSource property of the container control. In your linked article, you can see the following:
Tree View
The DataContext of the TreeView is the OrgTreeViewModel. We set the TreeView's ItemsSource to the Root property of the OrgTreeViewModel, which is the top-most node in the hierarchy.
It therefore seems as though you have a collection property named Root in your OrgTreeViewModel view model class. So, all you need to do to add a new item into your TreeView is to add a new data object into your Root collection. If that tutorial is any good, then the UI will automatically update, showing the new item.

Related

Use tree view to control list view scrolling behavior

I have a tree view that displays a structure of agenda items. The tree structure is merely for ui and usability purposes. The tree view is “flattened” into a linear list of agenda items. When an item is selected in the tree view, I would like to scroll the corresponding linear list item to the top of the listview.
The items in the listview are custom user controls.
I would like to use an attached behavior since I need to stay within MVVM. I have manly to issues. I need to be able to inject an index into the view and trigger the scroll itself by a command.
Any ideas?
Kind regards
This approach seem feasible but Im not getting how to pass an index to a behavior. https://marcominerva.wordpress.com/2014/09/30/scrolltobottom-behavior-for-listview-in-mvvm-based-universal-windows-apps/
This StackOverflow question should help you:
mvvm how to make a list view auto scroll to a new item in a list view
You shouldn't need to deal with an Index if you're using MVVM, just use the selected item instead. Ideally, your TreeView and ListView should be binding to the same items.

Custom Items control Approach

I need to build a custom items control in WPF, where the user can drag/drop the items. Normally I would just maintain a list of view models and use a data template to define how the items should be displayed (in this case, a button). But I am concerned that this will make the drag/drop difficult as the ItemsSource objects will be view model objects , not the actual button.
My other potential approach is when an object is added to the ItemsSource, create a button in c# and add it manually, that way I can access the button directly to do drag/drop.
What would your advice be?
I have solved my issue. I found a way to get the control being render from the data template

WPF add/edit/delete treeview nodes in MVVM style

I am new to MVVM and WPF treeview. I have done some research and have read the Josh Smith article on MVVM, and this, and this.
I think I have no problem creating the treeview in WPF. The thing is on my app, the left panel is the tree view, the right panel will display some properties of the selected tree view node, which user can click a button to edit the properties and save it to the data source (and potentially will affect the treeview item). In addition, user would be able to add/delete child node/grandchild node.
I can't seem to find any article/example to implement this using MVVM.
I am currently thinking that in the view models for the child node and grandchild node, I shall add a public property which points to a Usercontrol. The right panel will bind to the treeview's selected item's Usercontrol. The thing is, when user adds a child node/grand child node, the right panel will be used to let user fill in information and save. I am not sure whether it will affect the binding.
And other issues like editing the properties of a tree node will mean to copy all the child node info of the node to the new node and delete the old node from the tree and add the new node to the tree?
Can someone point me to any good articles on a similar implementation or give a rough idea on the issue that I should take note etc?
Thank you very much.
Angela
A lot depends on your setup but here is a way I have used before.
Note that you might need a ChildPropertyChanged event type of thing (I made that name up) to bubble up changes in the tree to the root of the tree.
To add a node
I created a ViewModel that contains:
a property for the tree data collection (probably a reference to the root node)
a property called NewNode.
a property called CurrentNode
a command that adds the NewNode to the CurrentNode: AddCommand
In the View:
Bind the TreeView to the tree data collection
Bind the SelectedItem of the TreeView to the CurrentNode
Bind the controls with the data for the new node to the NewNode properties
Bind a button to the AddCommand
In the AddCommand:
Add the NewNode to the CurrentNode and re-initialize the NewNode property.
To edit a node
In the ViewModel
add a command: UpdateCommand
add a command: EditCommand
In the View
bind some edit controls to the CurrentNode's properties (OneWay binding)
bind a button to the EditCommand
bind a button to the UpdateCommand
In the EditCommand:
make the edit controls and update button visible (I use a State property to bind to, see Extra below)
In the UpdateCommand:
write the values of the edit controls to the SelectedNode
hide the edit controls (I use a State property to bind to, see Extra below)
To delete a node
In the Viewmodel
add a DeleteCommand
In the DeleteCommand:
remove the CurrentNode from the collection
Extra
I have found it very useful to implement IEditableObject on the ViewModel of a Node.
Using the methods of this interface you can add a cancel button to reverse the EditCommand. By adding a State property to the ViewModel of a Node (New, Modified, Deleted) you can track changes, know what updates to send to the model/database and you can bind the View to it to show/hide elements.

Dynamically Load UserControl using MVVM Light Toolkit

I searched this site and i found 2 Links
how to load wpf usercontrol in MVVM pattern
MVVM-Light: Load UserControl into Window
but still i cant find the answer to my problem and this link
MVVM-Light Locator Pattern and Reusable UserControl
i Didn't Understood.... so here is i am stating my problem which might help others struggling like me ......
I have a MainWindow which has 2 parts one has a TreeView(a
UserControl) and the other Displays different user controls(named DisplayPanel).... just like windows Explorer.
The Display Panel on the Right side will display different user controls on Clicking nodes of tree view.
and my TreeView is Itself a user Control.
How can i make this composite UI Work using MVVM. Also I am planning to use MVVM light Toolkit. Does this have something that can help...
An Example will be great
Thanks... :)
Edit
My TreeView in a UserControl I made a dependency property in the UserControl which catches the selected Item fo the tree view so that i can use this dependency property to populate the required view in the "MainView" ContentControl binding....as you advised me in the comments. Everything is till now
Problem is that i want to display data contained in the the selected item and i cannot set the DataContext of the UserControls(which will be displayed in right hand side) to the selected item as then i will not be able to use my view model for the respective usercontrol for commands and other operations
I tried to solve this too.... i used the Mediator (Messenger) in my TreeViewUserControl view model to send a Message to the Usercontrol's(the one that i need to display) view model . Message will be passed whenever the item is selected in the tree view. and message contains the selected node. I forgot to mention i set the datacontext of the UserControl to its view model so that i could display data
But using this approach the problem is that when the I click a type of node for the first time the data is not populated but if the same type of node is clicked again its populated. What’s happening is that UserControls object is availabe when the the tree item is clicked for the first time and Mediator sends the message. So Mediator is not able to pass the message to the userControl view model.....
I totally do not have ne idea to solve this further.... is my way if displaying user control right or I should do something that else....totally confused.....
You could try defining a DataTemplate for each type in the TreeView's ItemsSource and instead of having a specific UserControl on the right side, just bind to the TreeView's SelectedItem. Alternatively, you could use a DataTemplateSelector.
Edited for OP's Edit
Did you do this?
MainWindow has TreeView whose ItemsSource=Binding MainVM.Items.
MainWindow has ContentControl whose Content=Binding TreeView.SelectedItem.
Somewhere in project, have ResourceDictionary where each possible type in MainVM.Items has a DataTemplate defined?
Which ViewModel (MainVM or ItemVM) are you trying to use and why can't you use it?

How do I bind a TreeView node to a specific view in an Explorer-like UI?

I'm sure there is a simple answer to this but I can't seem to find it. Most examples for binding TreeView nodes are about using a ListView to show the node's details. In the scenario I am working on I've got a TreeView data bound to an Xml document using a simple MVVM pattern. As each node is selected in the TreeView I want to show a different UserControl for the type of XmlNode being represented.
What is the best control for hosting the different View's? (ViewBox, Panel?)
What's the best way to bind the view to the Selection in the treeview?
Thanks
In an application I'm working on I have a similar scenario. You should use UserControl to host your views.
In the TreeView each item controls its own IsSelected state. Have you tried binding to the TreeView's SelectedItem property? What I actually did was create an attached property for the mouse double click, and bound it to a Command. I defined this binding in my HierarchicalDataTemplate.

Resources