Friday, 1 November 2013

WPF IVALUE CONVERTER - MANOHAR GOUD 9866550012


WPF Ivalue Converter


What is a Ivalue converter means ? 
I value converter is just middle tier converter which convert the source in specific format before its getting bind with target 
for example we save date in database in following format "2012-10-01 08:58:28.410" but suppose we want to display it as 'dd-mm-yyyy' format only then we require an Ivalue converter 
In following Example I am explaining the Ivalue converter use with WPF DataGrid
First start with Ivalueconverter class 
I just created a small class for same 

          public class DateConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value != null)
            {
                DateTime dt = (DateTime)value;
                return dt.ToString("dd-MMM-yyyy");
            }
            return string.Empty;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new Exception();
        }
    }

After that we create a .xaml and .xaml.cs coding
Just take one grid and bind that gris with data as below

.xaml

<Window x:Class="WpfDemo.IvalueConverterDemo"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:WpfDemo"
        Title="IvalueConverterDemo" Height="300" Width="300">
    <Window.Resources>
        <local:DateConverter x:Key="dateconverter" />
    </Window.Resources>
    <Grid>
        <DataGrid  x:Name="bdrBatches" AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn  Header="Batch ID" Binding="{Binding BatchID,Mode=TwoWay}" />
                <DataGridTextColumn  Header="Batch Name" Binding="{Binding BatcName,Mode=TwoWay}"/>
                <DataGridTextColumn  Header="Batch Start Date" Binding="{Binding BatchStartDate,Mode=TwoWay,Converter={StaticResource dateconverter}}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

       
.xaml.cs

                                         public partial class IvalueConverterDemo : Window
    {
        List<Batches> BatchesList = new List<Batches>();
        public IvalueConverterDemo()
        {
            InitializeComponent();
            BatchesList.Add(new Batches { BatchID = 1, BatcName = "Winter", BatchStartDate = Convert.ToDateTime("2011-09-01 08:58:28.410") });
            BatchesList.Add(new Batches { BatchID = 1, BatcName = "Summer", BatchStartDate = Convert.ToDateTime("2012-02-01 08:58:28.410") });
            BatchesList.Add(new Batches { BatchID = 1, BatcName = "Rainy", BatchStartDate = Convert.ToDateTime("2012-07-01 08:58:28.410") });
            bdrBatches.ItemsSource = BatchesList;
        }
    }

    public class Batches
    {
        public int BatchID { get; set; }
        public string BatcName { get; set; }
        public DateTime BatchStartDate { get; set; }
    }


The Result is look like as below
WPF Ivalue Converter Example















No comments:

Post a Comment