using System.Windows; using Microsoft.Xaml.Behaviors; namespace ShrlAlgoToolkit.Mvvm.Behaviors; /// /// ��ק�ļ��¼� /// /// DropFileBehaviors Data="{Binding FileNames,Mode=OneWayToSource}"���������Դ���Դ���ԣ�ͨ��Դ����ʵ��Mvvm /// ���ô�������AllowDrop�����ϲ㸲��һ�������Grid�����ô��ڵ�DropEnter��DropLeave(EventTrigger)�¼��������������Լ�����Drop�¼�������(ChangePropertyAction) class DropFileBehavior : Behavior { 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(); } }