Monday, November 30, 2009

Five Minute Silverlight 4 Aides-Memoire #1 – Drag and Drop

Build a hierarchical treeview of files and directories dropped from a Windows Explorer window. Requires elevated trust running out of browser to avoid security exceptions when accessing the directory information in the FileInfo objects returned from the e.Data.GetData call.


<Grid x:Name="LayoutRoot" AllowDrop="True" Drop="LayoutRoot_Drop">
my:TreeView x:Name="DirectoriesTreeView" Margin="50" Width="500" />

private void LayoutRoot_Drop(object sender, DragEventArgs e)
if (!Application.Current.HasElevatedPermissions e.Data == null)

FileInfo[] fileInfos = e.Data.GetData(DataFormats.FileDrop) as FileInfo[];
if (fileInfos == null)
List<FileInfo> sortedList = new List<FileInfo>(fileInfos);

sortedList.Sort((x, y) =>
if (x.Attributes == FileAttributes.Directory && y.Attributes != FileAttributes.Directory)
return -1;
if (y.Attributes == FileAttributes.Directory && x.Attributes != FileAttributes.Directory)
return 1;
return x.Name.CompareTo(y.Name);

foreach (FileInfo fileInfo in sortedList)
var newNode = new TreeViewItem { Header = fileInfo.Name };
if (!fileInfo.Exists)
ProcessDirectory(fileInfo, newNode);
private static void ProcessDirectory(FileSystemInfo fileSystemInfo, TreeViewItem currentNode)
DirectoryInfo directoryInfo = new DirectoryInfo(fileSystemInfo.FullName);

foreach (FileSystemInfo childFileSystemInfo in directoryInfo.EnumerateFileSystemInfos())
var newNode = new TreeViewItem { Header = childFileSystemInfo.Name };
if (childFileSystemInfo.Attributes == FileAttributes.Directory)
ProcessDirectory(childFileSystemInfo, newNode);

Wednesday, November 18, 2009

Silverlight 4

Quite unexpectedly, at least I wasn’t expecting it, today saw the announcement of the first beta of Silverlight 4 at PDC09 in Los Angeles. Barely has version 3 made it out there, but we have a new beta to play with together with another rev of Blend to support it (and .NET 4 in general).
A quick run through the main features in the beta:
  • Printing – Full access to the regular print facilities of the host machine – the print dialog and print preview features – so long to all the writable bitmaps and HTML bridge hacks
  • Webcam/microphone support – Subject to UAC/user confirmation, you can now programmatically get access to connected devices, capture video, stills and audio input streams
  • A Rich TextBox control – the WPF RTF control makes its appearance in Silverlight – embed other UI Elements, control text Runs and other formatting such as Text Decorations
  • Clipboard Access – Programmatic access to the clipboard – again subject to UAC
  • Right Mouse Context Menus – Full control of a customisable context menu – add whatever you want to it and respond to selection events
  • Drag-Drop Drop-Target support Silverlight apps can now act as drag-drop drop-targets receiving file information for the dropped file object(s)
  • MVVM Commanding – Finally proper implementation of the ICommand interface – no longer a need to kludge it via bindings to helper classes
  • IDataErrorInfo/Validation support – Integration of the support for the IDataErrorInfo interface in Silverlight’s DataBinding giving a validation framework for input controls to leverage in consistently surfacing data input errors with asynchronous data validation
  • String Formatting in Binding Extensions – As with WPF, Silverlight has been retrofitted with improved formatting support in the binding extension to give better control over stuff like date, time and other formatting for locales and long and short formatting
  • Out of Browser improvements: HTML content - Hosting of HTML content within the Silverlight app; Notifications (aka toast) support; Elevated Trust (again via UAC) access to the My Documents, My Videos, My Pictures special folders; Cross domain access and Full keyboard access in Full Screen mode
  • Better tooling and integration of RIA/ADO Services/WCF/MEF
Lots to check out and try out – exciting stuff!