简体   繁体   English

仅在使用鼠标时,如何允许拖放来排序项目?

[英]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. 我正在处理DragItemsStarting和Completed事件中需要的所有东西,并且一切正常。

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: 您可以使用UIViewSettings类获取当前的交互模式,并启用或禁用所需的功能,例如:

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. “ listView”是XAML代码中定义的ListView的名称。 You can use this code for example whenever the Point is over the ListView , or the Page is loaded or other time. 例如,当Point在ListView上方,页面加载或其他时候,都可以使用此代码。

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. 因此, GongSolutions.WPF.DragDrop库是WPF的拖放框架,您可以将其用于ListView,ListBox或任何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. 它已准备好MVVM,并且还提供了一个示例演示,您可以在其中查看运行中的库。

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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