ARTICLE

Custom Markup Extensions in Silverlight 5: Part 2

Posted by Mahadesh Mahalingappa Articles | Silverlight with C# August 21, 2011
In this article we are going to continue to learn about the Custom Markup Extensions in Silverlight 5 .
Reader Level:
Download Files:
 

In this post we will see how we can pass the parameters using Custom Markup Extensions .

Now that we have a understanding of how Custom Markup Extensions work in Silverlight lets quickly get into the code .

I modify my class FirstCME as shown as below :

public class FirstCME : IMarkupExtension<string>
    {
        public object value1 { get; set; }

        public object value2 { get; set; }

        public string ProvideValue(IServiceProvider serviceProvider)
        {
            double _val1 = Convert.ToDouble(value1);
            double _val2 = Convert.ToDouble(value2);

            return (_val1 + _val2).ToString();
        }
    }


Go ahead and modify the xaml to pass the value to the parameters . It would look like below :

<UserControl x:Class="SLCustomMarkup.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SLCustomMarkup"    
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth
="400">

    <Grid x:Name="LayoutRoot" Background="White">
        <TextBlock Text="{local:FirstCME value1=10,value2=20}"></TextBlock>
    </Grid>
</
UserControl>


Lets give it a run and see the output :

CMESil1.gif


The above xaml code could be modified as shown below : It would still work the same way . Here we pass the parameters in a Angular Bracket < > Format .

CMESil2.gif


Lets now give it a run :

CMESil3.gif

In the next post on Custom Markup Extensions we will see what are the advantages of Custom Markup Extensions . Thanks . Happy Coding.

Login to add your contents and source code to this article
post comment
     
COMMENT USING
PREMIUM SPONSORS
DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and add new content to existing PDF documents from within your applications.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter