Posted by: Zeeshan Amjad | December 23, 2009

Markup Extension return user define type


We have already seen how to extend the markup extension and return list of string. Now we are going to return list of user define data type. Now we are going to extend our program of getting information about file system. Here is our class to store file system information.

  1: public class FileSystemInfo
  2: {
  3:     public String Name
  4:     { get; set; }
  5: 
  6:     public long Length
  7:     { get; set; }
  8: 
  9:     public String CreationTime
 10:     { get; set; }
 11: 
 12:     public String Extension
 13:     { get; set; }
 14: 
 15:     public Boolean IsReadOnly
 16:     { get; set; }
 17: }
 18: 

We overload ProvideValue method and create list of FileSysteInfo.

  1: List<FileSystemInfo> items = new List<FileSystemInfo>();

Then we populate this list with appropriate information.

  1: FileInfo[] fiList = di.GetFiles();
  2: 
  3: foreach (FileInfo fi in fiList)
  4: {
  5:     FileSystemInfo fsi = new FileSystemInfo();
  6:     fsi.Name = fi.Name;
  7:     fsi.Extension = fi.Extension;
  8:     fsi.Length = fi.Length;
  9:     fsi.IsReadOnly = fi.IsReadOnly;
 10:     fsi.CreationTime = fi.CreationTime.ToString();
 11: 
 12:     items.Add(fsi);
 13: }
 14: 

Similarly here is code to store information about all the directories.

  1: DirectoryInfo[] diList = di.GetDirectories();
  2: 
  3: foreach (DirectoryInfo dInfo in diList)
  4: {
  5:     FileSystemInfo fsi = new FileSystemInfo();
  6:     fsi.Name = dInfo.Name;
  7:     fsi.CreationTime = dInfo.CreationTime.ToString();
  8:     fsi.Extension = dInfo.Extension;
  9: 
 10:     items.Add(fsi);
 11: }
 12: 

Here is a complete C# code of this project.

  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<FileSystemInfo> items = new List<FileSystemInfo>();
 38: 
 39:             DirectoryInfo di = new DirectoryInfo(directory);
 40: 
 41:             FileInfo[] fiList = di.GetFiles();
 42: 
 43:             foreach (FileInfo fi in fiList)
 44:             {
 45:                 FileSystemInfo fsi = new FileSystemInfo();
 46:                 fsi.Name = fi.Name;
 47:                 fsi.Extension = fi.Extension;
 48:                 fsi.Length = fi.Length;
 49:                 fsi.IsReadOnly = fi.IsReadOnly;
 50:                 fsi.CreationTime = fi.CreationTime.ToString();
 51: 
 52:                 items.Add(fsi);
 53:             }
 54: 
 55:             if (IncludeFolders == true)
 56:             {
 57:                 DirectoryInfo[] diList = di.GetDirectories();
 58: 
 59:                 foreach (DirectoryInfo dInfo in diList)
 60:                 {
 61:                     FileSystemInfo fsi = new FileSystemInfo();
 62:                     fsi.Name = dInfo.Name;
 63:                     fsi.CreationTime = dInfo.CreationTime.ToString();
 64:                     fsi.Extension = dInfo.Extension;
 65: 
 66:                     items.Add();
 67:                 }
 68:             }
 69: 
 70:             return items;
 71:         }
 72:     }
 73: 
 74:     /// <summary>
 75:     /// Interaction logic for Window1.xaml
 76:     /// </summary>
 77:     public partial class Window1 : Window
 78:     {
 79:         public Window1()
 80:         {
 81:             InitializeComponent();
 82:         }
 83:     }
 84: 
 85:     public class FileSystemInfo
 86:     {
 87:         public String Name
 88:         { get; set; }
 89: 
 90:         public long Length
 91:         { get; set; }
 92: 
 93:         public String CreationTime
 94:         { get; set; }
 95: 
 96:         public String Extension
 97:         { get; set; }
 98: 
 99:         public Boolean IsReadOnly
100:         { get; set; }
101:     }
102: }
103: 

 

And here is XAML file of this program.

  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="600">
  6:     <Window.Resources>
  7:         <Style x:Key="alternateColor" TargetType="{x:Type ListViewItem}">
  8:             <Style.Triggers>
  9:                 <Trigger Property="ItemsControl.AlternationIndex" Value="0">
 10:                     <Setter Property="Background" Value="LightGreen"></Setter>
 11:                 </Trigger>
 12:                 <Trigger Property="ItemsControl.AlternationIndex" Value="1">
 13:                     <Setter Property="Background" Value="LightYellow"></Setter>
 14:                 </Trigger>
 15:             </Style.Triggers>
 16:         </Style>
 17:     </Window.Resources>
 18:     <Grid>
 19:         <ListView Margin="5" Name="listView" AlternationCount="2"
 20:                   ItemContainerStyle="{StaticResource alternateColor}"
 21:                   ItemsSource="{Binding Source={local:DirectoryExtension Directory=Windows, IncludeFolders=true}}">
 22:             <ListView.View>
 23:                 <GridView>
 24:                     <GridViewColumn Width="Auto" Header="Name" DisplayMemberBinding="{Binding Path = Name}"/>
 25:                     <GridViewColumn Width="Auto" Header="Length" DisplayMemberBinding="{Binding Path = Length}"/>
 26:                     <GridViewColumn Width="Auto" Header="Creation Time" DisplayMemberBinding="{Binding Path = CreationTime}"/>
 27:                     <GridViewColumn Width="Auto" Header="Extension" DisplayMemberBinding="{Binding Path = Extension}"/>
 28:                     <GridViewColumn Width="Auto" Header="IsReadOnly" DisplayMemberBinding="{Binding Path = IsReadOnly}"/>
 29:                 </GridView>
 30:             </ListView.View>
 31:         </ListView>
 32:     </Grid>
 33: </Window>
 34: 

 

Here we also use the AlternateRowCount feature of .Net 3.5 SP1. Here is the output of this program.

ProcessMarkjupExtension

Advertisements

Responses

  1. […] The quickest method to do this is by inheriting one class from the Binding class. But we already saw that “ProvideValue” method of binding class is sealed so we cant override it. Here we are going to use the Markup extension. We already saw few examples of markup extension here, here and here.  […]


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: