0

my goal is to create a treeview with movable node. So, i find an inspiration at the following link: [Treeview with drag and drop] https://www.codeproject.com/Articles/55168/Drag-and-Drop-Feature-in-WPF-TreeView-Control .

My question is : how is possible to bind with command in viewmodel this events?

<treeview.itemcontainerstyle>

    <style targettype="{x:Type TreeViewItem}">        
      <EventSetter Event="TreeViewItem.DragOver"  Handler="treeView_DragOver"/>
      <EventSetter Event="TreeViewItem.Drop" Handler="treeView_Drop"/>
      <EventSetter Event="TreeViewItem.MouseMove" Handler="treeView_MouseMove"/> 
      <EventSetter Event="TreeViewItem.MouseDown" Handler="treeView_MouseDown"/>
    </style>              

 </treeview.itemcontainerstyle> 

I try this :

<EventSetter Event="TreeViewItem.DragOver"  Handler="{Binding DragOverCommand}"/>

and in relative viewmodel create DragOverCommand:

ICommand _cmdDragOverCommand;
        public ICommand DragOverCommand
        {
            get
            {
                if (_cmdDragOverCommand== null)
                {
                    _cmdDragOverCommand= new RelayCommand<tvMenuItem>(DragOverCommandExecute, CanDragOverCommandExecute);
                }
                return _cmdDragOverCommand;
            }
        }

        private bool CanDragOverCommandExecute(object param)
        {
            return true;
        }

        private void DragOverCommandExecute(object param)
        {
            try
            {
               ...     
            }
            catch (Exception ex)
            {
                ...
            }
        }

But at run time following error is shown: enter image description here

Have you an idea?

There are free graphic controls?

Thanks in advance

Cheers

rapp
  • 23
  • 5
  • 1
    Does this answer your question? [WPF calling commands via events](https://stackoverflow.com/questions/1048517/wpf-calling-commands-via-events) – Rekshino May 12 '20 at 07:42
  • Handling UI events using commands inside the view model is an anti-pattern in a MVVM context. Many believe in using `ICommand` alone is always conform with the MVVM pattern, but this is a huge misconception. The MVVM pattern is not about `ICommand`. MVVM is about separating view from the actual business application. All the events you are handling are _routed UI events_ with an UIElement as event source. UI events are always part of the UI logic. Your view model should not handle those events which transport UI data. View model should never have any interest in UI events at all. – BionicCode May 12 '20 at 11:08
  • You should handle those events in code-behind or using an attached behavior. Then extract the data models (e.g. `TreeViewItem.DataContext`) and use this data as command parameter to insert/move the item models in their collections or data structure. – BionicCode May 12 '20 at 11:09
  • Thanks All, and thank you for all the suggestions – rapp May 13 '20 at 12:53

0 Answers0