|
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;
using System.Windows.Input;
using System.IO;
namespace OpenLightGroupBehaviors
{
[System.ComponentModel.Description("Launches a Open File Dialog Box on Event Trigger")]
public class OpenFileDialogBoxBehavior : TargetedTriggerAction<Button>
{
// Properties
#region FileDialogDialogResultProperty
public static readonly DependencyProperty FileDialogDialogResultProperty =
DependencyProperty.Register("FileDialogDialogResultProperty",
typeof(FileInfo), typeof(OpenFileDialogBoxBehavior), 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(OpenFileDialogBoxBehavior), null);
public object ParentControl
{
get
{
return (object)base.GetValue(ParentControlProperty);
}
set
{
base.SetValue(ParentControlProperty, value);
}
}
#endregion
#region DialogFilterProperty
public static readonly DependencyProperty DialogFilterProperty =
DependencyProperty.Register("DialogFilter", typeof(string),
typeof(OpenFileDialogBoxBehavior), null);
public string DialogFilter
{
get
{
if (base.GetValue(DialogFilterProperty) == null)
{
return
"JPEG Files (*.jpg;*.jpeg)|*.jpg;*.jpeg|"
+ "Gif Files (*.gif)|*.gif|"
+ "DOC Files (*.doc)|*.doc|"
+ "DOCX Files (*.docx)|*.docx|"
+ "XLS Files (*.xls)|*.xls|"
+ "XLSX Files (*.xlsx)|*.xlsx|"
+ "PDF Files (*.pdf)|*.pdf|"
+ "PNG Files (*.png)|*.png|"
+ "TXT Files (*.txt)|*.txt";
}
else
{
return (string)base.GetValue(DialogFilterProperty);
}
}
set
{
base.SetValue(DialogFilterProperty, value);
}
}
#endregion
#region SelectedFileNameProperty
public static readonly DependencyProperty SelectedFileNameProperty =
DependencyProperty.Register("SelectedFileName", typeof(string),
typeof(OpenFileDialogBoxBehavior), null);
public string SelectedFileName
{
get
{
return (string)base.GetValue(SelectedFileNameProperty);
}
set
{
base.SetValue(SelectedFileNameProperty, value);
}
}
#endregion
// Operations
protected override void Invoke(object parameter)
{
OpenFileDialog objOpenFileDialog = new OpenFileDialog();
objOpenFileDialog.Filter = DialogFilter;
objOpenFileDialog.ShowDialog();
// Set the file to the FileInfo property
// this should be bound to the ViewModel by the designer
FileDialogDialogResult = objOpenFileDialog.File;
// Only proceed if a file was selected
if (FileDialogDialogResult != null)
{
SelectedFileName = ShortenFileName(FileDialogDialogResult.Name);
// Change to selected file Visual State
VisualStateManager.GoToState((Control)ParentControl, "FileSelectedState", true);
}
}
#region ShortenFileName
private string ShortenFileName(string filename)
{
string strFilename = "...";
if (filename.Length > 10)
{
strFilename = filename.Substring(0, 10) + " ...";
}
else
{
strFilename = filename;
}
return strFilename;
}
#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.