4 changed files with 99 additions and 0 deletions
@ -0,0 +1,71 @@ |
|||
using Avalonia.Controls; |
|||
using Avalonia.Input.DragDrop; |
|||
using Avalonia.Markup.Xaml; |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Text; |
|||
|
|||
namespace ControlCatalog.Pages |
|||
{ |
|||
public class DragAndDropPage : UserControl |
|||
{ |
|||
private TextBlock _DropState; |
|||
private TextBlock _DragState; |
|||
private Border _DragMe; |
|||
private int DragCount = 0; |
|||
|
|||
public DragAndDropPage() |
|||
{ |
|||
this.InitializeComponent(); |
|||
|
|||
_DragMe.PointerPressed += DoDrag; |
|||
|
|||
AddHandler(DragDrop.DropEvent, Drop); |
|||
AddHandler(DragDrop.DragOverEvent, DragOver); |
|||
} |
|||
|
|||
private async void DoDrag(object sender, Avalonia.Input.PointerPressedEventArgs e) |
|||
{ |
|||
DataObject dragData = new DataObject(); |
|||
dragData.Set(DataFormats.Text, $"You have dragged text {++DragCount} times"); |
|||
|
|||
var result = await DragDrop.DoDragDrop(dragData, DragDropEffects.Copy); |
|||
switch(result) |
|||
{ |
|||
case DragDropEffects.Copy: |
|||
_DragState.Text = "The text was copied"; break; |
|||
case DragDropEffects.Link: |
|||
_DragState.Text = "The text was linked"; break; |
|||
case DragDropEffects.None: |
|||
_DragState.Text = "The drag operation was canceled"; break; |
|||
} |
|||
} |
|||
|
|||
private void DragOver(object sender, DragEventArgs e) |
|||
{ |
|||
// Only allow Copy or Link as Drop Operations.
|
|||
e.DragEffects = e.DragEffects & (DragDropEffects.Copy | DragDropEffects.Link); |
|||
|
|||
// Only allow if the dragged data contains text or filenames.
|
|||
if (!e.Data.Contains(DataFormats.Text) && !e.Data.Contains(DataFormats.FileNames)) |
|||
e.DragEffects = DragDropEffects.None; |
|||
} |
|||
|
|||
private void Drop(object sender, DragEventArgs e) |
|||
{ |
|||
if (e.Data.Contains(DataFormats.Text)) |
|||
_DropState.Text = e.Data.GetText(); |
|||
else if (e.Data.Contains(DataFormats.FileNames)) |
|||
_DropState.Text = string.Join(Environment.NewLine, e.Data.GetFileNames()); |
|||
} |
|||
|
|||
private void InitializeComponent() |
|||
{ |
|||
AvaloniaXamlLoader.Load(this); |
|||
|
|||
_DropState = this.Find<TextBlock>("DropState"); |
|||
_DragState = this.Find<TextBlock>("DragState"); |
|||
_DragMe = this.Find<Border>("DragMe"); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,19 @@ |
|||
<UserControl xmlns="https://github.com/avaloniaui"> |
|||
<StackPanel Orientation="Vertical" Gap="4"> |
|||
<TextBlock Classes="h1">Drag+Drop</TextBlock> |
|||
<TextBlock Classes="h2">Example of Drag+Drop capabilities</TextBlock> |
|||
|
|||
<StackPanel Orientation="Horizontal" |
|||
Margin="0,16,0,0" |
|||
HorizontalAlignment="Center" |
|||
Gap="16"> |
|||
<Border BorderBrush="{DynamicResource ThemeAccentBrush}" BorderThickness="2" Padding="16" Name="DragMe"> |
|||
<TextBlock Name="DragState">Drag Me</TextBlock> |
|||
</Border> |
|||
<Border Background="{DynamicResource ThemeAccentBrush2}" Padding="16" |
|||
DragDrop.AllowDrop="True"> |
|||
<TextBlock Name="DropState">Drop some text or files here</TextBlock> |
|||
</Border> |
|||
</StackPanel> |
|||
</StackPanel> |
|||
</UserControl> |
|||
Loading…
Reference in new issue