0

I have a Listview. I have implemented MVVM pattern.

Now, in the View, I define the ItemContainerStyle for listview as below:

<ListView.ItemContainerStyle>
    <Style TargetType="ListViewItem">
             <Setter Property="IsSelected" Value="{Binding IsSelected}"/>
             <EventSetter Event="PreviewMouseLeftButtonDown" Handler="lstItemContact_MouseDown" />
             <EventSetter Event="PreviewMouseMove"  Handler="lstItemContact_MouseMove" />
    </Style>
</ListView.ItemContainerStyle>

and in code behind (xaml.cs) I have below events, for example, PreviewMouseLeftButtonDown:

    private void lstItemContact_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.LeftButton == MouseButtonState.Pressed)
        {

            // Get the current mouse position
            Point mousePos = e.GetPosition(null);
            Vector diff = _startPoint - mousePos;

            if (
    Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance ||
    Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance)
            {
                if (e.Source != null)
                {
                    List<DataModel> myList = new List<DataModel>();
                    foreach (DataModel Item in lvUsers.SelectedItems)
                    {
                        myList.Add(Item);
                    }

                    DataObject dataObject = new DataObject(myList);
                    DragDrop.DoDragDrop(lvUsers, dataObject, DragDropEffects.Move);
                }
            }
        }
    }

lstItemContact_MouseMove event is part of my implemented drag and drop function.

lvUsers is my listview in the View and my data model as you are supposing is DataModel.

It is working ok, but now I would like to move "lstItemContact_MouseMove" event from the view to my view model and use an ICommand (Maybe is possible to pass as parameter the listview object to the ICommand, I do not know). My problem is that I do not know how to get access to my listview (lvUsers) from View Model in order to pass the listview as parameter to the function:

DragDrop.DoDragDrop(lvUsers, dataObject, DragDropEffects.Move);

within the "lstItemContact_MouseMove" event.

So how can I do this?

2
  • 2
    Why do you want to implement the drag-and-drop operation in the view model in the first place? Commented Jul 3, 2017 at 13:01
  • @mm8 Well I am very new in MVVM wpf. Because I do not know if it is correct establish a dependency between view and data model. It is a good practice? does it respect the MVVM principles? Commented Jul 3, 2017 at 13:04

1 Answer 1

2

I think the best course of action in your case can be:

  1. Leave it in the code-behind and don't worry about it.
  2. Make a Behavior out of it.

Your ViewModel shouldn't be concerned with UI matters like Drag & Drop, but a separate Behavior is a good place to put it.

You can also read up about it at MSDN or CodeProject

Sign up to request clarification or add additional context in comments.

1 Comment

I like very much your solution with behaviour. I will try to do it. If I have any problem I will tell you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.