如何创建树视图删除添加编辑节点mvvm [英] How do I create tree view remove add edit node mvvm

查看:81
本文介绍了如何创建树视图删除添加编辑节点mvvm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我正在尝试在WPF中创建树视图,

我需要能够编辑节点中的文本

删除删除节点点击

添加新节点



我找到了一些解决方案,包括editbale文本框

和inputBinding键。但是,它不是完整的mvvvm模式而且工作不正常,所以我正在寻找新的解决方案,示例项目或单独的解决方案。



未来感谢,

Jenya

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

推荐答案

执行此操作的最佳方法是为树视图项创建自定义模型。在此模型中,您将拥有所有属性,包括ICommand属性,例如添加新节点,删除节点等等。例如,您的模型将具有



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

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
}





在你的xaml中,因为你的itemsource将是这个type,您的事件的绑定,例如上下文菜单或鼠标按钮单击,您仍然可以使用System.Windows.Interactivity执行纯mvvm ,然后你可以将命令绑定到这个函数。



对于我的删除逻辑,我设置了一个委托/事件系统来通知viewmodel中的itemssource删除节点因为itemsource属性在viewmodel中。我想你也可以只将bool IsSelected属性添加到你的模型中,并将命令逻辑放在viewmodel中并搜索itemssource以查看哪个被选中并将其删除。



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.


这篇关于如何创建树视图删除添加编辑节点mvvm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆