Click here to Skip to main content
15,890,717 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,
I am trying to create tree view in WPF,
I need to be able to edit the text in the node
remove nodes on delete click
add new node

I found a few solutions for it, including editbale textbox
and inputBinding key. However, it's not full mvvvm pattern and not working properly, so I am looking for a new solution, sample project or apart solutions.

Thanks ahead,
Jenya
Posted
Comments
Suvendu Shekhar Giri 25-Nov-15 6:24am    
Here is an example of Treeview in WPF using MVVM
Simplifying the WPF TreeView by Using the ViewModel Pattern[^]

You may need to add other features to it as per your requirement.

1 solution

The best way to do this is to create a custom model for your treeview item. In this model you will have all of your properties including your ICommand properties such as add new node, remove node so on so forth. For example, your model will have

C#
private ICommand _RemoveNodeCommand;
public ICommand RemoveNodeCommand
{
    get
    {
        if (_RemoveNodeCommand == null)
        {
         _RemoveNodeCommand=new ExecutePath(param=>this.RemoveNodeOperation(this);
        }
        return _RemoveNodeCommand;
    }
}

public void RemoveNodeOperation(YourModelName node)
{
    //Remove logic
}


In your xaml since your itemsource will be of this type, the bindings for your event such as a context menu, or a mouse button click, which you can still do pure mvvm using System.Windows.Interactivity, then you can bind the command to this function.

For my remove logic I set up a delegate/event system to notify the itemssource in the viewmodel of the node removal since the itemsource property is in the viewmodel. I suppose you could also just add a bool IsSelected property to your model, and put the command logic in the viewmodel and do a search through the itemssource to see which is selected and remove it that way as well.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900