I have an application in which the listview items are being dragged and dropped. Its already done with Mouse Events. I want to change it to touch with minimal changes.. I read a post somewhere asking to handle something called GlobalHandler and associate other events with that so that its generic like I did. In that case how can I check if the event is Left button down or left button up etc. Also will touch work with DragEnter event? Here is my code at the moment. Please help.,
<ListView
Name="RunSetupListView"
Grid.Row="0"
Grid.Column="0"
Grid.ColumnSpan="3"
Style="{StaticResource RunSetupDisplayListViewStyle}"
ItemsSource="{Binding RunSetupInfoList}"
AlternationCount="100"
LayoutUpdated="RunSetupListView_LayoutUpdated"
PreviewMouseLeftButtonDown="GlobalHandler"
PreviewMouseLeftButtonUp="GlobalHandler"
MouseMove="GlobalHandler"
DragEnter="RunSetupListView_DragEnter"
DragOver="RunSetupListView_DragOver"
Drop="RunSetupListView_Drop"
DragLeave="RunSetupListView_DragLeave"/>
Here is the code in the cs file
private void GlobalHandler(object sender, InputEventArgs e)
{
//How to check if the event triggered from Touchup, touch down etc. Please help.
if(e.RoutedEvent==Mouse.MouseDownEvent)||//How can I add if its from touch here?
}
private void RunSetupListView_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Do not even allow a drag/drop event to start if the mouse is pressed
// on an area that is a tree view item
var listViewItem =
VisualTreeHelperUtils.FindParent<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem != null)
{
SelectedDragItem = listViewItem;
// Log start point
StartPoint = e.GetPosition(null);
}
e.Handled = true;
}
private void RunSetupListView_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// Reest in case only a click occurs in the tree view WITHOUT a drag event
StartPoint = null;
SelectedDragItem = null;
e.Handled = true;
}
private void RunSetupListView_MouseMove(object sender, MouseEventArgs e)
{
if (!(sender is ListView))
return;
// only for left button down and if we received a mouse down
// event in the listview... sometimes this event will still get processed
// even if you click outside the listview but then drag the mouse into the listview
if (e.LeftButton == MouseButtonState.Pressed && StartPoint != null && SelectedDragItem != null)
{
var mousePos = e.GetPosition(null);
var diff = StartPoint.Value - mousePos;
// Once the drag has been dragged far enough... prevents a drag from happening
// from simply clicking the item
if ((Math.Abs(diff.X) > SystemParameters.MinimumHorizontalDragDistance
|| Math.Abs(diff.Y) > SystemParameters.MinimumVerticalDragDistance) &&
!_inDragMode)
{
var listView = sender as ListView;
var listViewItem =
VisualTreeHelperUtils.FindParent<ListViewItem>((DependencyObject)e.OriginalSource);
if (listViewItem == null)
{
return;
}
m_AdornerLayer = InitializeAdornerLayer(listViewItem, listView);
UpdateDragAdornerLocation(e.GetPosition(listView));
mousePoint = mousePos;
_inDragMode = true;
DataObject dragData = new DataObject(listViewItem.Content as DNA2RunInfoDisplay);
listView.CaptureMouse();
DragDrop.DoDragDrop(listViewItem, dragData, DragDropEffects.Move);
_inDragMode = false;
// Reset this here... the mouse up event DOES NOT get raised if we do a drag/drop effect
StartPoint = null;
SelectedDragItem = null;
}
}