简体   繁体   中英

How to allow to drag drop to sort items only when using a mouse?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM