Here is an easy to use FileDragDropBehavior that you can use in your XAML to implement dragging and dropping of files in your Silverlight 4 Application
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.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(); | |
} |
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
<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> |
Just put the class in your project, reference the namespace in your XAML file and add the behavior to where you want to drop a file.
Jacob
No comments:
Post a Comment