Posted by: Zeeshan Amjad | January 27, 2011

String Formatting Markup Extension


WPF 3.5 SP1 introduced StringFormat parameter in the binding where we can define the format of the string. Its usage is very simple. Here is one simple usage of it.

Code Snippet
  1. <Binding Path="Name" StringFormat="{} Mr {0}   Jr"/>

 

Now we are going to do the similar thing using the markup extension. Here we are going to use the same technique which we used when making the range converter here. I.e. define the value converter return it from the markup extension. Here is our value converter.

Code Snippet
  1. public class StringFormattingConverter : IValueConverter
  2. {
  3.     public StringFormattingConverter()
  4.     {
  5.     }
  6.  
  7.     public StringFormattingConverter(String prefix, String suffix)
  8.     {
  9.         Prefix = prefix;
  10.         Suffix = suffix;
  11.     }
  12.  
  13.     public String Prefix
  14.     { get; set; }
  15.  
  16.     public String Suffix
  17.     { get; set; }
  18.  
  19.     public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  20.     {
  21.         return Prefix + " " + value + " " + Suffix;
  22.     }
  23.  
  24.     public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  25.     {
  26.         throw new NotImplementedException();
  27.     }
  28. }

 

Now we are going to use this value converter in our markup extension. Here is a code of our markup extension.

Code Snippet
  1. public class StringFormattingExtension : MarkupExtension
  2. {
  3.     public String Prefix
  4.     { get; set; }
  5.  
  6.     public String Suffix
  7.     { get; set; }
  8.  
  9.     public override object ProvideValue(IServiceProvider serviceProvider)
  10.     {
  11.         return new StringFormattingConverter(Prefix, Suffix);
  12.     }
  13. }

 

Here is the usage of our markup extension.

Code Snippet
  1. <Binding Path="Name" Converter="{local:StringFormatting Prefix=Mr,Suffix=Jr}"/>

 

The main difference between this approach and using the given StringFormat parameter is that we can define our own business logic there. We can return the different value of string depends on the binding variable. In addition we can still pass the converter parameter and use it. But unfortunately, we can do binding on command parameter because it is not a dependency property. Therefore if we write this code then it will throw an exception at run time.

Code Snippet
  1. <Binding Path="Name" ConverterParameter="{Binding Path=Gender}"
  2.         Converter="{local:StringFormatting Prefix=Mr,Suffix=Jr}"/>

 

Here is complete C# code of the program.

Code Snippet
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Windows;
  6. using System.Windows.Controls;
  7. using System.Windows.Data;
  8. using System.Windows.Documents;
  9. using System.Windows.Input;
  10. using System.Windows.Media;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Navigation;
  13. using System.Windows.Shapes;
  14. using System.Collections.ObjectModel;
  15. using System.Windows.Markup;
  16. using System.Globalization;
  17.  
  18. namespace WpfMarkupExtension
  19. {
  20.     /// <summary>
  21.     /// Interaction logic for MainWindow.xaml
  22.     /// </summary>
  23.     public partial class MainWindow : Window
  24.     {
  25.         private ObservableCollection<Person> names = new ObservableCollection<Person>();
  26.  
  27.         public MainWindow()
  28.         {
  29.             InitializeComponent();
  30.  
  31.             names.Add(new Person("Alex", "Male"));
  32.             names.Add(new Person("Smith", "Male"));
  33.             names.Add(new Person("Bob", "Male"));
  34.             names.Add(new Person("Jake", "Male"));
  35.             names.Add(new Person("Alice", "Female"));
  36.  
  37.             DataContext = names;
  38.         }
  39.     }
  40.  
  41.     public class StringFormattingConverter : IValueConverter
  42.     {
  43.         public StringFormattingConverter()
  44.         {
  45.         }
  46.  
  47.         public StringFormattingConverter(String prefix, String suffix)
  48.         {
  49.             Prefix = prefix;
  50.             Suffix = suffix;
  51.         }
  52.  
  53.         public String Prefix
  54.         { get; set; }
  55.  
  56.         public String Suffix
  57.         { get; set; }
  58.  
  59.         public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
  60.         {
  61.             return Prefix + " " + value + " " + Suffix;
  62.         }
  63.  
  64.         public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
  65.         {
  66.             throw new NotImplementedException();
  67.         }
  68.     }
  69.  
  70.     public class StringFormattingExtension : MarkupExtension
  71.     {
  72.         public String Prefix
  73.         { get; set; }
  74.  
  75.         public String Suffix
  76.         { get; set; }
  77.  
  78.         public override object ProvideValue(IServiceProvider serviceProvider)
  79.         {
  80.             return new StringFormattingConverter(Prefix, Suffix);
  81.         }
  82.     }
  83.  
  84.     public class Person
  85.     {
  86.         public Person()
  87.         {
  88.         }
  89.  
  90.         public Person(String name, String gender)
  91.         {
  92.             Name = name;
  93.             Gender = gender;
  94.         }
  95.  
  96.         public String Name
  97.         { get; set; }
  98.  
  99.         public String Gender
  100.         { get; set; }
  101.     }
  102. }

 

Here is complete XAML code of the program.

Code Snippet
  1. <Window x:Class="WpfMarkupExtension.MainWindow"
  2.        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4.        xmlns:local="clr-namespace:WpfMarkupExtension"
  5.        Title="String Formatting Markup Extension" Height="300" Width="400">
  6.     <Grid>
  7.         <ListBox Margin="5" ItemsSource="{Binding}">
  8.             <ListBox.ItemTemplate>
  9.                 <DataTemplate>
  10.                     <TextBlock Margin="2" >
  11.                         <TextBlock.Text>
  12.                             <Binding Path="Name" Converter="{local:StringFormatting Prefix=Mr,Suffix=Jr}"/>
  13.                         </TextBlock.Text>
  14.                     </TextBlock>
  15.                 </DataTemplate>
  16.             </ListBox.ItemTemplate>
  17.         </ListBox>
  18.     </Grid>
  19. </Window>

 

This is the out put of this program.

StringFormatMarkupExtension

Advertisements

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: