|
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Input;
using System.IO;
namespace OpenLightGroupBehaviors
{
[System.ComponentModel.Description("Allows files to be dropped for upload")]
public class DropFilesToUploadBehavior : Behavior<UIElement>
{
private double DropUIElementOpacity;
// Properties
#region FileDialogDialogResultProperty
public static readonly DependencyProperty FileDialogDialogResultProperty =
DependencyProperty.Register("FileDialogDialogResultProperty",
typeof(FileInfo), typeof(DropFilesToUploadBehavior), null);
public FileInfo FileDialogDialogResult
{
get
{
return (FileInfo)base.GetValue(FileDialogDialogResultProperty);
}
set
{
base.SetValue(FileDialogDialogResultProperty, value);
}
}
#endregion
#region ParentControlProperty
public static readonly DependencyProperty ParentControlProperty =
DependencyProperty.Register("ParentControlProperty",
typeof(object), typeof(DropFilesToUploadBehavior), null);
public object ParentControl
{
get
{
return (object)base.GetValue(ParentControlProperty);
}
set
{
base.SetValue(ParentControlProperty, value);
}
}
#endregion
#region SelectedFileNameProperty
public static readonly DependencyProperty SelectedFileNameProperty =
DependencyProperty.Register("SelectedFileName", typeof(string),
typeof(DropFilesToUploadBehavior), null);
public string SelectedFileName
{
get
{
return (string)base.GetValue(SelectedFileNameProperty);
}
set
{
base.SetValue(SelectedFileNameProperty, value);
}
}
#endregion
protected override void OnAttached()
{
base.OnAttached();
// Save the opacity of the element
DropUIElementOpacity = AssociatedObject.Opacity;
// Turn on allow drop
AssociatedObject.AllowDrop = true;
// Attach event handlers
AssociatedObject.DragOver += new DragEventHandler(DropUIElement_DragOver);
AssociatedObject.DragLeave += new DragEventHandler(DropUIElement_DragLeave);
AssociatedObject.Drop += new DragEventHandler(DropUIElement_Drop);
}
protected override void OnDetaching()
{
base.OnDetaching();
// Attach event handlers
AssociatedObject.DragOver -= new DragEventHandler(DropUIElement_DragOver);
AssociatedObject.DragLeave -= new DragEventHandler(DropUIElement_DragLeave);
AssociatedObject.Drop -= new DragEventHandler(DropUIElement_Drop);
}
#region DropUIElement_DragOver
void DropUIElement_DragOver(object sender, DragEventArgs e)
{
// If you hover over the drop point, change it's opacity so users will
// have some indication that they can drop
AssociatedObject.Opacity = (double)0.5;
}
#endregion
#region DropUIElement_DragLeave
void DropUIElement_DragLeave(object sender, DragEventArgs e)
{
// Return opacity to normal
AssociatedObject.Opacity = DropUIElementOpacity;
}
#endregion
#region DropUIElement_Drop
void DropUIElement_Drop(object sender, DragEventArgs e)
{
// Return opacity to normal
AssociatedObject.Opacity = DropUIElementOpacity;
// If there is something being dropped upload it
if (e.Data != null)
{
FileInfo[] Dropfiles = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
// Set the file to the FileInfo property
// this should be bound to the ViewModel by the designer
FileDialogDialogResult = Dropfiles[0];
// Check File Extension
if (!CheckFileExtension(FileDialogDialogResult.Extension))
{
// Show error
MessageBox.Show("Only .gif, .jpg, .jpeg, .doc, .docx, .xls, .xlsx, .pdf, .png, .txt, .wav files may be used.");
// Clear the selected file
FileDialogDialogResult = null;
}
else
{
SelectedFileName = ShortenFileName(FileDialogDialogResult.Name);
// Change to selected file Visual State
VisualStateManager.GoToState((Control)ParentControl, "FileSelectedState", true);
}
}
}
#endregion
#region ShortenFileName
private string ShortenFileName(string filename)
{
string strFilename = "...";
if (filename.Length > 10)
{
strFilename = filename.Substring(0, 10) + " ...";
}
else
{
strFilename = filename;
}
return strFilename;
}
#endregion
#region CheckFileExtension
private bool CheckFileExtension(string strExtension)
{
if (
string.Compare(Path.GetExtension(strExtension).ToLower(), ".gif") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".jpg") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".jpeg") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".doc") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".docx") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".xls") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".xlsx") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".pdf") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".txt") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".png") != 0
& string.Compare(Path.GetExtension(strExtension).ToLower(), ".wav") != 0
)
{
return false;
}
else
{
return true;
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.