Posted by: Zeeshan Amjad | December 22, 2009

Using Array Extension

WPF has already defined lots of markup extension classes, such as Binding, StaticResource, Null, Array etc. Now we are going to use the Array extension and make a simple program of it. Here is a class diagram of all markup extension classes defined in WPF.


Here is a simple code to create array using array markup extension.

  1: <Window.Resources>
  2: 	<x:Array x:Key="myArray" Type="sys:String">
  3: 		<sys:String>Maryland</sys:String>
  4: 		<sys:String>California</sys:String>
  5: 		<sys:String>Arizona</sys:String>
  6: 		<sys:String>Washington</sys:String>
  7: 		<sys:String>Virginia</sys:String>
  8: 		<sys:String>Delaware</sys:String>
  9: 	</x:Array>
 10: </Window.Resources>

We can use this array to bind it with list box, combo box, list view etc. We can assign this array into ItemsSource property of list box. Here is complete XAML code of the program.

  1: <Window x:Class="WpfArrayExtension.Window1"
  2:     xmlns=""
  3:     xmlns:x=""
  4:     xmlns:sys="clr-namespace:System;assembly=mscorlib"
  5:     Title="Array Extension" Height="300" Width="300">
  6:     <Window.Resources>
  7:         <x:Array x:Key="myArray" Type="sys:String">
  8:             <sys:String>Maryland</sys:String>
  9:             <sys:String>California</sys:String>
 10:             <sys:String>Arizona</sys:String>
 11:             <sys:String>Washington</sys:String>
 12:             <sys:String>Virginia</sys:String>
 13:             <sys:String>Delaware</sys:String>
 14:         </x:Array>
 15:     </Window.Resources>
 16:     <Grid>
 17:         <ListBox Margin="5" ItemsSource="{StaticResource myArray}" 
 18:                  HorizontalContentAlignment="Stretch">
 19:             <ListBox.ItemTemplate>
 20:                 <DataTemplate>
 21:                     <Border Margin="3" BorderBrush="Brown" BorderThickness="1" CornerRadius="5">
 22:                         <Border.Background>
 23:                             <LinearGradientBrush>
 24:                                 <GradientStop Offset="0" Color="Wheat"/>
 25:                                 <GradientStop Offset="0.5" Color="LightYellow"/>
 26:                                 <GradientStop Offset="1" Color="Wheat"/>
 27:                             </LinearGradientBrush>
 28:                         </Border.Background>
 29:                         <TextBlock Margin="3" FontWeight="Bold" Text="{Binding}"/>
 30:                     </Border>
 31:                 </DataTemplate>
 32:             </ListBox.ItemTemplate>
 33:         </ListBox>
 34:     </Grid>
 35: </Window>

This is the output of this program.



Leave a Reply

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

You are commenting using your 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


%d bloggers like this: