Posted by: Zeeshan Amjad | May 31, 2010

Using markup extension to set the value of text block


We have seen several example of markup extension. Now we are going to make one more markup extension, but this time use this with text block. We are also going to do some calculation inside the markup extension. Here is our markup extension class.

[MarkupExtensionReturnType(typeof(string))]
public class TestExtension : MarkupExtension
{
    public String FirstNo
    { get; set; }

    public String SecondNo
    { get; set; }

    public override object ProvideValue(IServiceProvider serviceProvider)
    {
        int first = Convert.ToInt32(FirstNo);
        int second = Convert.ToInt32(SecondNo);

        int gcd = CalculateGCDInternal(first, second);

        String strRet = gcd.ToString();

        return strRet;
    }

    private int CalculateGCDInternal(int x, int y)
    {
        if (x == 0)
            return 0;

        while (y != 0)
        {
            if (x > y)
                x = x - y;
            else
                y = y - x;
        }

        return x;
    }
}     

Here is its usage in XAML.

<Window x:Class="WpfMarkupExtension.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:WpfMarkupExtension"
    Title="Markup Extension" Height="300" Width="300">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <TextBlock Grid.Column="0" Margin="5" VerticalAlignment="Center">
            GCD of 12 and 45 is
        </TextBlock>
        <TextBlock Grid.Column="1" Margin="5" VerticalAlignment="Center"
                   Text="{local:TestExtension 
                   FirstNo=12, SecondNo=45}"/>
    </Grid>
</Window>

Rest of the coding of the program is very simple. Here is a complete C# coding 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.ComponentModel;
using System.Diagnostics;

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

    [MarkupExtensionReturnType(typeof(string))]
    public class TestExtension : MarkupExtension
    {
        public String FirstNo
        { get; set; }

        public String SecondNo
        { get; set; }

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            int first = Convert.ToInt32(FirstNo);
            int second = Convert.ToInt32(SecondNo);

            int gcd = CalculateGCDInternal(first, second);

            String strRet = gcd.ToString();

            return strRet;
        }

        private int CalculateGCDInternal(int x, int y)
        {
            if (x == 0)
                return 0;

            while (y != 0)
            {
                if (x > y)
                    x = x - y;
                else
                    y = y - x;
            }

            return x;
        }
    }     
}

Here is the output of the program.

MarkupExtensionOutput_3

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: