Created
December 19, 2013 07:21
-
-
Save karno/8035572 to your computer and use it in GitHub Desktop.
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
using System.Windows; | |
using System.Windows.Interactivity; | |
namespace Karno.Xac.Sample | |
{ | |
public sealed class DragAcceptBehavior : Behavior<FrameworkElement> | |
{ | |
public DragAcceptDescription Description | |
{ | |
get { return (DragAcceptDescription)GetValue(DescriptionProperty); } | |
set { SetValue(DescriptionProperty, value); } | |
} | |
public static readonly DependencyProperty DescriptionProperty = | |
DependencyProperty.Register("Description", typeof(DragAcceptDescription), | |
typeof(DragAcceptBehavior), new PropertyMetadata(null)); | |
protected override void OnAttached() | |
{ | |
this.AssociatedObject.PreviewDragOver += AssociatedObject_DragOver; | |
this.AssociatedObject.PreviewDrop += AssociatedObject_Drop; | |
base.OnAttached(); | |
} | |
protected override void OnDetaching() | |
{ | |
this.AssociatedObject.PreviewDragOver -= AssociatedObject_DragOver; | |
this.AssociatedObject.PreviewDrop -= AssociatedObject_Drop; | |
base.OnDetaching(); | |
} | |
void AssociatedObject_DragOver(object sender, DragEventArgs e) | |
{ | |
var desc = Description; | |
if (desc == null) | |
{ | |
e.Effects = DragDropEffects.None; | |
e.Handled = true; | |
return; | |
} | |
desc.OnDragOver(e); | |
e.Handled = true; | |
} | |
void AssociatedObject_Drop(object sender, DragEventArgs e) | |
{ | |
var desc = Description; | |
if (desc == null) | |
{ | |
e.Effects = DragDropEffects.None; | |
e.Handled = true; | |
return; | |
} | |
desc.OnDrop(e); | |
e.Handled = true; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment