Click here to Skip to main content
15,893,588 members
Articles / Programming Languages / C# 4.0

Silverlight Simple Drag And Drop / Or Browse View Model / MVVM File Upload Control

Rate me:
Please Sign up or sign in to vote.
4.96/5 (28 votes)
25 Jun 2011Ms-PL3 min read 107.1K   2.9K   54  
An example of a Silverlight Drag And Drop / Or Browse File Upload Control using View Model / MVVM
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.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer (Senior) http://ADefWebserver.com
United States United States
Michael Washington is a Microsoft MVP. He is a ASP.NET and
C# programmer.
He is the founder of
AiHelpWebsite.com,
LightSwitchHelpWebsite.com, and
HoloLensHelpWebsite.com.

He has a son, Zachary and resides in Los Angeles with his wife Valerie.

He is the Author of:

Comments and Discussions