Skip to content

Instantly share code, notes, and snippets.

@cairey
Created July 20, 2012 15:07
Show Gist options
  • Save cairey/3151214 to your computer and use it in GitHub Desktop.
Save cairey/3151214 to your computer and use it in GitHub Desktop.
Drag over control to command bahvior
public class DragOverControlToCommandBehavior : CommandBehaviorBase<Control>
{
public DragOverControlToCommandBehavior(Control element)
: base(element)
{
element.AllowDrop = true;
element.DragOver += ElementDragOver;
}
private void ElementDragOver(object sender, DragEventArgs e)
{
this.ExecuteCommand();
}
}
public class DragOverControlToCommandDependency
{
public static readonly DependencyProperty CommandProperty =
DependencyProperty.RegisterAttached("Command", typeof(ICommand), typeof(DragOverControlToCommandDependency), new PropertyMetadata(OnSetCommandCallback));
public static readonly DependencyProperty CommandParameterProperty =
DependencyProperty.RegisterAttached("CommandParameter", typeof(object), typeof(DragOverControlToCommandDependency), new PropertyMetadata(OnSetCommandParameterCallback));
public static readonly DependencyProperty PatientDragOverControlToCommandBehaviorProperty =
DependencyProperty.RegisterAttached("DragOverControlToCommandBehavior", typeof(DragOverControlToCommandBehavior), typeof(DragOverControlToCommandDependency), null);
public static ICommand GetCommand(DependencyObject obj)
{
return (ICommand)obj.GetValue(CommandProperty);
}
public static void SetCommand(DependencyObject obj, ICommand value)
{
obj.SetValue(CommandProperty, value);
}
public static object GetCommandParameter(DependencyObject obj)
{
return obj.GetValue(CommandParameterProperty);
}
public static void SetCommandParameter(DependencyObject obj, object parameter)
{
obj.SetValue(CommandParameterProperty, parameter);
}
public static DragOverControlToCommandBehavior GetPatientDragOverControlToCommandBehavior(DependencyObject obj)
{
return (DragOverControlToCommandBehavior)obj.GetValue(PatientDragOverControlToCommandBehaviorProperty);
}
public static void SetPatientDragOverControlToCommandBehavior(DependencyObject obj, DragOverControlToCommandBehavior value)
{
obj.SetValue(PatientDragOverControlToCommandBehaviorProperty, value);
}
private static void OnSetCommandCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as Control;
if (element != null)
{
var behavior = GetOrCreateBehavior(element);
behavior.Command = e.NewValue as ICommand;
}
}
private static void OnSetCommandParameterCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var element = d as Control;
if (element != null)
{
var behavior = GetOrCreateBehavior(element);
behavior.CommandParameter = e.NewValue;
}
}
private static DragOverControlToCommandBehavior GetOrCreateBehavior(Control element)
{
var behavior = element.GetValue(PatientDragOverControlToCommandBehaviorProperty) as DragOverControlToCommandBehavior;
if (behavior == null)
{
behavior = new DragOverControlToCommandBehavior(element);
element.SetValue(PatientDragOverControlToCommandBehaviorProperty, behavior);
}
return behavior;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment