将需求启用拖拽的控件实现PointerPressed
方法
<TextBlock Text="{Binding Name}" PointerPressed="DoDrag" Background="Transparent"/>
private async void DoDrag(object sender, Avalonia.Input.PointerPressedEventArgs e)
{
dynamic Data = sender;
if (Data.DataContext.GetType() != typeof(FlowTable)) return;
var dragData = new DataObject();
dragData.Set("TreeView", (FlowTable)Data.DataContext);//填写待拖拽的数据
var result = await DragDrop.DoDragDrop(e, dragData, DragDropEffects.Copy);
}
将接收拖拽的控件开启允许数据拖拽
<ItemsControl Items="{Binding Item}" DragDrop.AllowDrop="True" Background="Transparent"/>
全局响应拖拽方法(开启了DragDrop.AllowDrop
都会响应到此方法)
AddHandler(DragDrop.DropEvent, Drop);//定义全局拖拽事件
AddHandler(DragDrop.DragOverEvent, DragOver);
private void DragOver(object sender, DragEventArgs e)
{
}
private void Drop(object sender, DragEventArgs e)
{
dynamic Target = e.Source;
dynamic TargetData = Target.DataContext;//目标数据
if (TargetData.GetType() == typeof(FlowTabModel))
{
dynamic Source = e.Data.Get("TreeView");//源数据
}
else if (TargetData.GetType() == typeof(FlowView))
{
}
}