Posted by: Zeeshan Amjad | August 6, 2009

Markup Extension in WPF Revisited


As we saw in the previous discussion that Markup extension is basically an addition in XAML. Using this method we are extending XAML. In WPF we have to inherit our class from MarkuExtension class. WPF already give lot of markup extensions. In fact data binding is also a markup extension. Here is a class diagram to show the markup extension already given by WPF.

MarkupExtension

In our previous discussion we created one markup extension to display the currently running process. Now we are going to further explore our discussion and see how can we set the properties in the markup extension.

This time we are going to create a markup extension to display the file list on the C drive. There are two properties of this markup extension class. First one is the string property to set the path of the folder in which we are interested. If we do not set this property then by default it will return the files on the root folder. And second is a Boolean property. If set it to true then it will display the subfolders also in addition to files.

Here is a simple implementation of these properties.

  1:         private String directory;
  2: 
  3:         public String Directory
  4:         {
  5:             get 
  6:             { 
  7:                 return directory; 
  8:             }
  9:             set
 10:             {
 11:                 directory = "C:\";
 12:                 directory += value;
 13:             }
 14:         }
 15: 
 16:         public bool IncludeFolders
 17:         { get; set; }
 18: 

Implementation of ProvideValue function is very simple. It simply return the file list of whatever is set at the directory property and if IncludeFolders property is set to true then it will further add the folders list. Here is a simple implementation of this function.

  1:         public override object ProvideValue(IServiceProvider serviceProvider)
  2:         {
  3:             List<string> items = new List<string>();
  4: 
  5:             DirectoryInfo di = new DirectoryInfo(directory);
  6: 
  7:             FileInfo[] fiList = di.GetFiles();
  8: 
  9:             foreach (FileInfo fi in fiList)
 10:             {
 11:                 items.Add(fi.FullName);
 12:             }
 13: 
 14:             if (IncludeFolders == true)
 15:             {
 16:                 DirectoryInfo[] diList = di.GetDirectories();
 17: 
 18:                 foreach (DirectoryInfo dInfo in diList)
 19:                 {
 20:                     items.Add(dInfo.FullName);
 21:                 }
 22:             }
 23: 
 24:             return items;
 25:         }
 26: 

The usage of this markup extension is also very simple. We can set the individual properties by using its names. Here is the usage of this markup extension.

  1: <ListBox ItemsSource="{Binding Source={local:DirectoryExtension Directory=Windows, IncludeFolders=true}}" />

Now lets everything together. Here is a complete C# program of this Markup extension.

  1: using System;
  2: using System.IO;
  3: using System.Collections.Generic;
  4: using System.Windows;
  5: using System.Windows.Markup;
  6: 
  7: namespace xaml
  8: {
  9:     [MarkupExtensionReturnType(typeof(string))]
 10:     public class DirectoryExtension : MarkupExtension
 11:     {
 12:         private String directory;
 13: 
 14:         public String Directory
 15:         {
 16:             get 
 17:             { 
 18:                 return directory; 
 19:             }
 20:             set
 21:             {
 22:                 directory = "C:\";
 23:                 directory += value;
 24:             }
 25:         }
 26: 
 27:         public bool IncludeFolders
 28:         { get; set; }
 29: 
 30:         public DirectoryExtension()
 31:         {
 32:             Directory = "C:\";
 33:         }
 34: 
 35:         public override object ProvideValue(IServiceProvider serviceProvider)
 36:         {
 37:             List<string> items = new List<string>();
 38: 
 39:             DirectoryInfo di = new DirectoryInfo(directory);
 40: 
 41:             FileInfo[] fiList = di.GetFiles();
 42: 
 43:             foreach (FileInfo fi in fiList)
 44:             {
 45:                 items.Add(fi.FullName);
 46:             }
 47: 
 48:             if (IncludeFolders == true)
 49:             {
 50:                 DirectoryInfo[] diList = di.GetDirectories();
 51: 
 52:                 foreach (DirectoryInfo dInfo in diList)
 53:                 {
 54:                     items.Add(dInfo.FullName);
 55:                 }
 56:             }
 57: 
 58:             return items;
 59:         }
 60:     }
 61: 
 62:     /// <summary>
 63:     /// Interaction logic for Window1.xaml
 64:     /// </summary>
 65:     public partial class Window1 : Window
 66:     {
 67:         public Window1()
 68:         {
 69:             InitializeComponent();
 70:         }
 71:     }
 72: }
 73: 

And here is complete XAML code of its usages.

  1: <Window x:Class="xaml.Window1"
  2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4:     xmlns:local="clr-namespace:xaml"
  5:     Title="Process Markup Extension" Height="400" Width="400">
  6:     <Grid>
  7:         <ListBox ItemsSource="{Binding Source={local:DirectoryExtension Directory=Windows, IncludeFolders=true}}" />
  8:     </Grid>
  9: 
 10: </Window>
 11: 

Here is the output of the program

MarkupExtensionOutput_2

Advertisements

Responses

  1. […] already studied about markup extension here and here. We already know that in markup extension we have to overload the “ProvideValue” […]

  2. […] we are going to use the Markup extension. We already saw few examples of markup extension here, here and […]

  3. […] already saw the example of Process markup extension and Directory extension. Now we are going to make one more markup extension that is color markup extension. Let’s make a […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: