Reputation: 4567
I'm trying to implement the drag-drop sort feature into my app and I'm having a little problem. Let's say I have something like this:
<ListView ItemsSource="{x:Bind ViewModel.Source, Mode=OneWay}"
ScrollViewer.VerticalScrollBarVisibility="Auto"
CanReorderItems="True"
CanDragItems="True"
AllowDrop="True"
DragItemsStarting="MyList_DragItemsStarting"
DragItemsCompleted="MyList_OnDragItemsCompleted"/>
I'm handling all the stuff I need from the DragItemsStarting and Completed events, and it all works fine.
The problem though is that I have some other code that is triggered when the user is using a touch screen (like swipe actions and stuff) and I want the drag/drop operation to only be available when using a mouse.
I'm not seeing a place where I can switch depending on the pointer device type, and I don't know where should I look.
Is there a way to do that? Has anyone implemented something like this and can explain how to code that?
Thank you for your help!
Sergio
Upvotes: 0
Views: 463
Reputation: 16652
You can use UIViewSettings class to get the current interaction mode, and enable or disable your wanted function for example like this:
switch (UIViewSettings.GetForCurrentView().UserInteractionMode)
{
case UserInteractionMode.Mouse:
listView.AllowDrop = true;
listView.CanDragItems = true;
listView.CanReorderItems = true;
break;
case UserInteractionMode.Touch:
default:
listView.AllowDrop = false;
listView.CanDragItems = false;
listView.CanReorderItems = false;
break;
}
"listView" is the name of ListView
which is defined in XAML code. You can use this code for example whenever the Point is over the ListView
, or the Page is loaded or other time.
Upvotes: 1
Reputation: 14611
So there are many solutions for this. But why always reinvent the wheel. There is a cool library which you can use for your stuff.
So the GongSolutions.WPF.DragDrop library is a drag'n'drop framework for WPF and you can use it for ListView, ListBox or whatever ItemsControls.
<ListView ItemsSource="{Binding YourCollection}"
dd:DragDrop.IsDragSource="True"
dd:DragDrop.IsDropTarget="True"
dd:DragDrop.DropHandler="{Binding}" />
It's MVVM ready and a sample demo is also there where you can see the library in action.
Hope that helps.
Upvotes: 0