Here is an implementation of the View Model Style pattern to create a simple Silverlight File Manager. This Silverlight project is not a full featured file manager, but, it actually works and hopefully demonstrates a non-trivial example of a View Model Style Silverlight project.
|
using System;
using System.ComponentModel;
using System.Windows.Input;
using System.Collections.ObjectModel;
using MVMFileManager.FileManager;
namespace MVVMFileManager
{
public class MainViewModel : INotifyPropertyChanged
{
public MainViewModel()
{
// Set the command property
SetFilesCommand = new DelegateCommand(SetFiles, CanSetFiles);
// Set default values
SilverlightFolders = new ObservableCollection<SilverlightFolder>();
SilverlightFiles = new ObservableCollection<SilverlightFile>();
// Pass a reference of this class to the method to get the Folders
SilverlightFolderAndFiles objSilverlightFolderAndFiles = new SilverlightFolderAndFiles();
objSilverlightFolderAndFiles.GetFolders(this);
}
#region Commanding
public ICommand SetFilesCommand { get; set; }
public void SetFiles(object param)
{
// Get the Folder selected
SilverlightFolder objSilverlightFolder = (SilverlightFolder)param;
// Clear the file list
SilverlightFiles = new ObservableCollection<SilverlightFile>();
// Pass a reference of this class to the method to get the Files
SilverlightFolderAndFiles objSilverlightFolderAndFiles = new SilverlightFolderAndFiles();
objSilverlightFolderAndFiles.GetFiles(this, objSilverlightFolder);
}
private bool CanSetFiles(object param)
{
return true;
}
#endregion
#region Folders
private ObservableCollection<SilverlightFolder> _SilverlightFolders;
public ObservableCollection<SilverlightFolder> SilverlightFolders
{
get { return _SilverlightFolders; }
private set
{
if (SilverlightFolders == value)
{
return;
}
_SilverlightFolders = value;
this.NotifyPropertyChanged("SilverlightFolders");
}
}
#endregion
#region Files
private ObservableCollection<SilverlightFile> _SilverlightFiles;
public ObservableCollection<SilverlightFile> SilverlightFiles
{
get { return _SilverlightFiles; }
private set
{
if (SilverlightFiles == value)
{
return;
}
_SilverlightFiles = value;
this.NotifyPropertyChanged("SilverlightFiles");
}
}
#endregion
#region INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String info)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(info));
}
}
#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.