Tuesday 28 February 2012

IValueConverter In Silverlight:-


While binding data the IValueConverter gives flexibility.For this we have to  implement IValueConverter and use Convert function.

->Create a User class.

public class User
 {
        public string FirstName { get; set; }

        public string LastName { get; set; }

        public int Age { get; set; }

        public string Address { get; set; }
 }

->Create a Converter class and implement IValueConverter methods.

public class Converter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            //Check the parameter.
            if (parameter.ToString() == "Message")
            {
                //Create custom message here by using first name.
                return "Hai " + value.ToString();
            }
            else
            {
                return null;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

->Add the Converter class in xaml as static resource.
<UserControl x:Class="SilverlightIValueConverter.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"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400" Loaded="UserControl_Loaded"
    xmlns:con="clr-namespace:SilverlightIValueConverter">
    <UserControl.Resources>
        <con:Converter x:Name="converter"></con:Converter>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <ListBox Name="lstUsers">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions >
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                            <ColumnDefinition Width="100"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <TextBlock Grid.Column="0" Name="txtFirstName" Text="{Binding Path=FirstName}"></TextBlock>
                        <TextBlock Grid.Column="1" Name="txtLastName" Text="{Binding Path=LastName}"></TextBlock>
                        <TextBlock Grid.Column="2" Name="txtAge" Text="{Binding Path=Age}"></TextBlock>
                        <TextBlock Grid.Column="3" Name="txtAddress" Text="{Binding Path=Address}"></TextBlock>
                        <TextBlock Grid.Column="4" Name="txtMessage" Text="{Binding Path=FirstName,Converter={StaticResource converter},ConverterParameter=Message}"></TextBlock>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</UserControl>

->In user control load event bind the listbox.
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            //User list
            List<User> lstUser = new List<User>();

            //Adding users.
            lstUser.Add(new User() { FirstName = "David", LastName = "Jackson", Age = 23, Address = "WC" });
            lstUser.Add(new User() { FirstName = "ANDERSON", LastName = "ANDI", Age = 23, Address = "WC" });
            lstUser.Add(new User() { FirstName = "ANDRA", LastName = "Jackson", Age = 23, Address = "WC" });

            //Set the item source here.
            lstUsers.ItemsSource = lstUser;
        }


Let me know, if you have any feedback. Mail me for source code. Enjoy reading my articles…
sekhartechblog@gmail.com

No comments:

Post a Comment