Menu

CONVERTER IN SILVERLIGHT

CONVERTER
Converters can change data from one type to another, translate data based on cultural information, or modify other aspects of the presentation. For examples of some typical converter scenarios, see “Data Conversion” in Data Binding Overview.
The IValueConverter interface and then implement the Convert and ConvertBack methods.
Convert : Modifies the source data before passing it to the target for display in the UI.
ConvertBack : Modifies the target data before passing it to the source object. This method is called only in TwoWay bindings.

Getting Started
Download Source code:=>ConverterTestApplication.rar

You need to have Silverlight 3.0 or 4.0 framework installed on your PC for implementation of converter.
We can create Sliverlight application for Converter.

1.  Create a new Silverlight application. Open Visual Studio 2010 > File > New Project > Select the language (C# or VB.NET) > Select ‘Silverlight’ in the Project Types > from the templates, select Silverlight Application from the installed templates and enter a name for the application
2.   On clicking OK, Let’s structure our project as below

3. First create the Model “Test”


namespace ConverterTestApplication.Model
{
public class Test
{
private string _StrLen;
public string StrLen
{
get
{
return _StrLen;
}
set
{
_StrLen = value;
}
}
}
}

4. We need the IValueConverter, which will be able to convert the string length and return password Strength code like this

using System;

using System.Globalization;

using System.Windows;

using System.Windows.Data;

namespace ConverterTestApplication.Converter

{

public class Converter12 : IValueConverter

{

#region IValueConverter Members

public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

string str = value.ToString();

string message = "";

int len = str.Length;

if (len > 0 && len 4 && len 8 && len 12)

{

message = "strong";

}

return message;

}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

{

return null;

}

#endregion

}

}

5. In the view, we need to include the xmlns namespace for the ViewModel and the Converter. Create the StaticResource of MainViewModel and testConverter, as shown below and set the DataContext of the LayoutRoot Grid to the MainViewModel.
MainModule

6. Here is the full code of the XAML page i.e. View:

<UserControl x:Class="ConverterTestApplication.MainPage"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

xmlns:viewModels="clr-namespace:ConverterTestApplication.Model"

xmlns:converter="clr-namespace:ConverterTestApplication.Converter"

mc:Ignorable="d"

d:DesignHeight="300" d:DesignWidth="400">

<UserControl.Resources>

<viewModels:Test x:Key="mainViewModel"/>

<converter:Converter12 x:Key="testConverter"/>

</UserControl.Resources>

<Grid x:Name="LayoutRoot" Background="White"  DataContext="{StaticResource mainViewModel}">

<Grid.RowDefinitions>

<RowDefinition Height="auto"/>

<RowDefinition Height="auto"/>

<RowDefinition Height="auto"/>

<RowDefinition Height="244*" />

</Grid.RowDefinitions>

<Grid.ColumnDefinitions>

<ColumnDefinition Width="auto"/>

<ColumnDefinition Width="auto"/>

<ColumnDefinition Width="auto"/>

<ColumnDefinition Width="93*" />

</Grid.ColumnDefinitions>

<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="3">Converter Demo Application(Check Password Strength)</TextBlock>

<TextBlock Grid.Row="1" Grid.Column="0">Password</TextBlock>

<TextBox x:Name="txtStr" Width="100" Grid.Row="1" Grid.Column="1" Text="{Binding StrLen,Mode=TwoWay}"  />

<TextBlock Grid.Row="2" Grid.Column="0">Strength :</TextBlock>

<TextBlock Grid.Row="2" Grid.Column="1" Text="{Binding Text,Converter={StaticResource testConverter},ElementName=txtStr}"/>

</Grid>

</UserControl>

Referral Link :=>

http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx

http://weblogs.asp.net/psheriff/archive/2010/09/24/using-value-converters-in-silverlight.aspx

http://weblogs.asp.net/dwahlin/archive/2009/08/15/so-what-s-a-silverlight-value-converter-anyway.aspx

http://www.kunal-chowdhury.com/2010/11/using-valueconverter-to-showhide.html

No comments

Leave a Reply


− 7 = 1

Categories