Posted by: Zeeshan Amjad | August 3, 2009

Markup Extension in WPF


We can extend the XAML by using the MarkupExtension class of .Net. This is an abstract class. WPF already provide lots of classes inherited from it. In fact in WPF data binding is doing with markup extension class. This abstract class is directly inherited from Object class not from DependencyObject class and it has one abstract method name “ProvideValue”. Every class inherited from MarkupExtension must override it. Here is a class diagram of MarkupExtension class.

MarkupExtension

Lets try to make our own XAML markup extension. It is very similar to define Custom attributes in C#. In our simple Markup extension we are going to display the name of all of the running process. Here is a simple implementation of this.

  1:     [MarkupExtensionReturnType(typeof(string))]
  2:     public class ProcessExtension : MarkupExtension
  3:     {
  4:         public override object ProvideValue(IServiceProvider serviceProvider)
  5:         {
  6:             List<string> items = new List<string>();
  7: 
  8:             Process[] processList = Process.GetProcesses();
  9: 
 10:             foreach (Process process in processList)
 11:             {
 12:                 items.Add(process.ProcessName);
 13:             }
 14: 
 15:             return items;
 16:         }
 17:     }
 18: 

We define the attribute of markup extension return type, which is optional and we can skip this. Now we are going to display all the values return by this extension on one list box. Here is the usage of it.

  1: <ListBox ItemsSource="{Binding Source={local:ProcessExtension}}" />

Here local is the name space where this markup extension is defined. Here is the complete C# code of the application.

  1: using System;
  2: using System.Collections.Generic;
  3: using System.Windows;
  4: using System.Windows.Markup;
  5: using System.Diagnostics;
  6: 
  7: namespace xaml
  8: {
  9:     [MarkupExtensionReturnType(typeof(string))]
 10:     public class ProcessExtension : MarkupExtension
 11:     {
 12:         public override object ProvideValue(IServiceProvider serviceProvider)
 13:         {
 14:             List<string> items = new List<string>();
 15: 
 16:             Process[] processList = Process.GetProcesses();
 17: 
 18:             foreach (Process process in processList)
 19:             {
 20:                 items.Add(process.ProcessName);
 21:             }
 22: 
 23:             return items;
 24:         }
 25:     }
 26: 
 27:     /// <summary>
 28:     /// Interaction logic for Window1.xaml
 29:     /// </summary>
 30:     public partial class Window1 : Window
 31:     {
 32:         public Window1()
 33:         {
 34:             InitializeComponent();
 35:         }
 36:     }
 37: }
 38: 

And here is the complete XAML coding of this.

  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:ProcessExtension}}" />
  8:     </Grid>
  9: 
 10: </Window>
 11: 

Here is the output of this program.

MarkupExtensionOutput

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. […] it. Here we are going to use the Markup extension. We already saw few examples of markup extension here, here and […]


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: