Skip to content

Instantly share code, notes, and snippets.

@jgable
Created February 24, 2011 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jgable/842877 to your computer and use it in GitHub Desktop.
Save jgable/842877 to your computer and use it in GitHub Desktop.
An easy to implement DragDropBehavior for incorporating drag and drop in your silverlight 4 application; also includes a FileDragDropBehavior for easy file drag and drop
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interactivity;
/// <summary>
/// A simple file drag and drop behavior.
/// </summary>
public class FileDragDropBehavior : DragDropBehavior<FileInfo[]>
{
/// <summary>
/// The <see cref="FilesDroppedCommand" /> dependency property's name.
/// </summary>
public const string FilesDroppedCommandPropertyName = "FilesDroppedCommand";
/// <summary>
/// Gets or sets the value of the <see cref="FilesDroppedCommand" />
/// property. This is a dependency property.
/// </summary>
public ICommand FilesDroppedCommand
{
get
{
return (ICommand)GetValue(FilesDroppedCommandProperty);
}
set
{
SetValue(FilesDroppedCommandProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="FilesDroppedCommand" /> dependency property.
/// </summary>
public static readonly DependencyProperty FilesDroppedCommandProperty = DependencyProperty.Register(
FilesDroppedCommandPropertyName,
typeof(ICommand),
typeof(FileDragDropBehavior),
new PropertyMetadata(null));
/// <summary>
/// Determines if this drop event should run.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.DragEventArgs"/> instance containing the event data.</param>
/// <returns>True if the event should run, otherwise false</returns>
protected override bool ShouldDropEventRun(object sender, DragEventArgs e)
{
return this.FilesDroppedCommand != null;
}
/// <summary>
/// Called when [dropped data].
/// </summary>
/// <param name="files">The files.</param>
protected override void OnDroppedData(FileInfo[] files)
{
if (files == null)
return;
if (this.FilesDroppedCommand.CanExecute(files))
this.FilesDroppedCommand.Execute(files);
}
/// <summary>
/// Gets the data string.
/// </summary>
/// <returns></returns>
protected override string GetDataString()
{
return DataFormats.FileDrop;
}
}
public abstract class DragDropBehavior<TData> : Behavior<FrameworkElement>
{
protected override void OnAttached()
{
AssociatedObject.AllowDrop = true;
AssociatedObject.Drop += AssociatedObject_Drop;
base.OnAttached();
}
protected override void OnDetaching()
{
AssociatedObject.Drop -= AssociatedObject_Drop;
base.OnDetaching();
}
protected void AssociatedObject_Drop(object sender, DragEventArgs e)
{
if(!ShouldDropEventRun(sender, e))
return;
var dataName = GetDataString();
if(!e.Data.GetDataPresent(dataName))
return;
var data = (TData)e.Data.GetData(dataName);
OnDroppedData(data);
}
/// <summary>
/// Called when [dropped data].
/// </summary>
/// <param name="data">The data.</param>
protected abstract void OnDroppedData(TData data);
/// <summary>
/// Determines if a drop event should run; use this to check for null values and what-not.
/// </summary>
/// <param name="sender">The sender.</param>
/// <param name="e">The <see cref="System.Windows.DragEventArgs"/> instance containing the event data.</param>
/// <returns>True if should run, otherwise false</returns>
protected virtual bool ShouldDropEventRun(object sender, DragEventArgs e)
{
return true;
}
/// <summary>
/// When overriden, gets the data string to call e.Data.GetData(...) with.
/// </summary>
/// <returns>The data string to call GetData(...) with.</returns>
protected abstract string GetDataString();
}
<Border
Width="300"
Height="200"
Margin="0 20 0 20"
Background="LightGray"
x:Name="DropArea">
<i:Interaction.Behaviors>
<con:FileDragDropBehavior
FilesDroppedCommand="{Binding FilesDroppedCmd}" />
</i:Interaction.Behaviors>
<TextBlock
FontWeight="Bold" FontSize="20"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Drag Files Here" />
</Border>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment