Posted by: Zeeshan Amjad | May 28, 2010

Color Markup Extension


We 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 simple markup extension of color class.

[MarkupExtensionReturnType(typeof(string))]
public class ColorExtension : MarkupExtension
{
    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        List<String> colorsList = new List<String>();

        String[] colors = Enum.GetNames(typeof(KnownColor));

        foreach (String color in colors)
        {
            colorsList.Add(color);
        }

        return colorsList;
    }
}

It is important that we have to include the reference of System.Drawing class.

using System.Drawing;

Now we can use markup extension. Here is XAML file the program

<Window x:Class="WpfColorMarkupExtension.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfColorMarkupExtension"
    Title="Color Markup Extension" Height="300" Width="300">
    <Grid>
        <ListBox Margin="5" HorizontalContentAlignment="Stretch" ItemsSource="{Binding Source={local:ColorExtension}}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Margin="2, 0" Grid.Column="0" Text="{Binding}" />
                        <Rectangle Margin="2, 0" Grid.Column="1" Fill="{Binding}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Window>

Here is complete C# code of the program.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Markup;
using System.Drawing;

namespace WpfColorMarkupExtension
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    [MarkupExtensionReturnType(typeof(string))]
    public class ColorExtension : MarkupExtension
    {
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            List<String> colorsList = new List<String>();

            String[] colors = Enum.GetNames(typeof(KnownColor));

            foreach (String color in colors)
            {
                colorsList.Add(color);
            }

            return colorsList;
        }
    }
}

 

This is the output of the program.

ColorMarkupExtension

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: