拖动代码
让我们看看控件的实现以及我们如何处理拖动项。
public class GridViewEx : GridView
{
/// <summary>
/// Initializes a new instance of the <see cref="GridViewEx"/> control.
/// </summary>
public GridViewEx()
{
// see attached sample
}
private void GridViewEx_DragItemsStarting(object sender, DragItemsStartingEventArgs e)
{
// see attached sample
}
/// <summary>
/// Stores dragged items into DragEventArgs.Data.Properties["Items"] value.
/// Override this method to set custom drag data if you need to.
/// </summary>
protected virtual void OnDragStarting(DragItemsStartingEventArgs e)
{
// see attached sample
}
The control has several fields which store the indices of several active items during the drag/drop process. The OnDragStarting
事件将拖动的项目存储到
DragEventArgs.Data.Properties[“Items”] 值。你会覆盖这个
如果需要,设置自定义拖动数据的方法。
当用户拖动一个项目时,我们需要提示如果该项目被放下,该项目将被放置在哪里。标准的 GridView 处理这个
通过滑动相邻的项目。我们将执行这个确切的
在 GridViewEx 中表现自己,因为我们需要考虑案例
其中GridView不支持drop。
/// <summary>
/// Shows reoder hints while custom dragging.
/// </summary>
protected override void OnDragOver(DragEventArgs e)
{
// see attached sample }
private int GetDragOverIndex(DragEventArgs e)
{
// see attached sample
}
Dropping Code
Next, let’s look at the code that handles dropping.
We have to override GridView.OnDrop method which is called every time when an end-user drops an item to the new location. Our override
处理标准 GridView 所做的任何 ItemsPanel 的删除
不支持丢弃。
/// <summary>
/// Handles drag and drop for cases when it is not supported by the Windows.UI.Xaml.Controls.GridView control
/// </summary>
protected override async void OnDrop(DragEventArgs e)
{
// see attached sample
}
The OnDrop method includes logic for moving items from one group to another when grouping is enabled, and for new group creation if it
由最终用户操作请求。