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?