Files
Shrlalgo.RvKits/Sai.Toolkit.Mvvm/Behaviors/DropFileBehavior.cs
2024-09-22 11:05:41 +08:00

45 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Windows;
using Microsoft.Xaml.Behaviors;
/// <summary>
/// 拖拽文件事件
/// </summary>
/// <example>DropFileBehaviors Data="{Binding FileNames,Mode=OneWayToSource}"从依赖属性传到源属性通过源属性实现Mvvm</example>
/// <remarks>设置窗口属性AllowDrop在上层覆盖一个面板如Grid设置窗口的DropEnter和DropLeave(EventTrigger)事件控制其显隐以及控制Drop事件后隐藏(ChangePropertyAction)</remarks>
class DropFileBehavior : Behavior<FrameworkElement>
{
public string[] Data
{
get { return (string[])GetValue(DataProperty); }
set { SetValue(DataProperty, value); }
}
public static readonly DependencyProperty DataProperty =
DependencyProperty.Register(nameof(Data), typeof(string[]), typeof(DropFileBehavior), new PropertyMetadata(null));
protected override void OnAttached()
{
AssociatedObject.AllowDrop = true;
AssociatedObject.Drop += DropHandler;
}
private void DropHandler(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
Data = (string[])e.Data.GetData(DataFormats.FileDrop);
}
}
protected override void OnDetaching()
{
AssociatedObject.Drop -= DropHandler;
}
protected override void OnChanged()
{
base.OnChanged();
}
}