Ejemplo n.º 1
0
        public NodeViewModelBase(T model)
        {
            Model = model;

            Model.CollectionChanged += Model_CollectionChanged;
            foreach (var item in Model)
            {
                var vm = (INodeViewModel)ViewModelFactory.Create(item);
                if (vm != null)
                {
                    vm.Parent = this;
                    Children.Add(vm);
                }
            }

            Delete = new RelayCommand(arg =>
            {
                if (Parent != null)
                {
                    Parent.Remove(this);
                }
            });

            Copy = new RelayCommand(arg =>
            {
                var xml = NodeSerializer.save(this.Model);
                Clipboard.SetText(xml);
            });
            Paste = new RelayCommand(arg =>
            {
                var txt  = Clipboard.GetText();
                var root = NodeSerializer.load(txt);
                if (root != null)
                {
                    this.Model.Add(root);
                }
            });
            Cut = new RelayCommand(arg =>
            {
                var xml = NodeSerializer.save(this.Model);
                Clipboard.SetText(xml);
                if (Parent != null)
                {
                    Parent.Remove(this);
                }
            });

            Create = new RelayCommand(arg =>
            {
                ContextMenu.Items.Clear();
                foreach (var de in NodeTypes)
                {
                    var mi     = CreateHierarchicalMenuItem(ContextMenu.Items, de.Value.Path.Split(new[] { '/' }), 0);
                    mi.Command = new RelayCommand((arg2) =>
                    {
                        var ca = arg2 as CreateArgs;

                        var node = Activator.CreateInstance(ca.Type) as Prototype.Behaviortree.Node;
                        ca.Node.DataModel.Add(node);
                    });
                    mi.CommandParameter = new CreateArgs {
                        Type = de.Value.Type, Node = this
                    };
                }
                ContextMenu.IsOpen = true;
            });

            CheckEmpty();
        }