This repository has been archived by the owner on Nov 1, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #143 from alexandrehtrb/samples/datagrid-drag-and-…
…drop samples: add sample for DataGrid drag-and-drop
- Loading branch information
Showing
5 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
samples/DragAndDropSample/Behaviors/ItemsDataGridDropHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
using Avalonia.Controls; | ||
using Avalonia.Input; | ||
using Avalonia.VisualTree; | ||
using Avalonia.Xaml.Interactions.DragAndDrop; | ||
using DragAndDropSample.ViewModels; | ||
|
||
namespace DragAndDropSample.Behaviors; | ||
|
||
public class ItemsDataGridDropHandler : DropHandlerBase | ||
{ | ||
private bool Validate<T>(DataGrid dg, DragEventArgs e, object? sourceContext, object? targetContext, bool bExecute) where T : ItemViewModel | ||
{ | ||
if (sourceContext is not T sourceItem | ||
|| targetContext is not MainWindowViewModel vm | ||
|| dg.GetVisualAt(e.GetPosition(dg)) is not Control targetControl | ||
|| targetControl.DataContext is not T targetItem) | ||
{ | ||
return false; | ||
} | ||
|
||
var items = vm.Items; | ||
var sourceIndex = items.IndexOf(sourceItem); | ||
var targetIndex = items.IndexOf(targetItem); | ||
|
||
if (sourceIndex < 0 || targetIndex < 0) | ||
{ | ||
return false; | ||
} | ||
|
||
switch (e.DragEffects) | ||
{ | ||
case DragDropEffects.Copy: | ||
{ | ||
if (bExecute) | ||
{ | ||
var clone = new ItemViewModel() { Title = sourceItem.Title + "_copy" }; | ||
InsertItem(items, clone, targetIndex + 1); | ||
} | ||
return true; | ||
} | ||
case DragDropEffects.Move: | ||
{ | ||
if (bExecute) | ||
{ | ||
MoveItem(items, sourceIndex, targetIndex); | ||
} | ||
return true; | ||
} | ||
case DragDropEffects.Link: | ||
{ | ||
if (bExecute) | ||
{ | ||
SwapItem(items, sourceIndex, targetIndex); | ||
} | ||
return true; | ||
} | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
public override bool Validate(object? sender, DragEventArgs e, object? sourceContext, object? targetContext, object? state) | ||
{ | ||
if (e.Source is Control && sender is DataGrid dg) | ||
{ | ||
return Validate<ItemViewModel>(dg, e, sourceContext, targetContext, false); | ||
} | ||
return false; | ||
} | ||
|
||
public override bool Execute(object? sender, DragEventArgs e, object? sourceContext, object? targetContext, object? state) | ||
{ | ||
if (e.Source is Control && sender is DataGrid dg) | ||
{ | ||
return Validate<ItemViewModel>(dg, e, sourceContext, targetContext, true); | ||
} | ||
return false; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters