Recommended

Skip Navigation LinksHome / Search

Search

 
Results Per Page

Found 1 result for value convertor.
Date between: <not defined> and <not defined>
Search in: News , Articles , Tips , Shows , Showcase , Books

Order by Publish Date   Ascending Title   Rating  

  • 1 comments  /  posted by  Martin Mihaylov  on  Sep 09, 2008 (more than a year ago)

    First let's explain what the convertors can be used for. Imagine you bind to an object's property, but the property is not formatted to your likings. In this case you can use converters. For example we bind to the PublishDate property of a Book object and want the date to be formatted like this - "dd MMM, yyyy".

    Book myBook = new Book();
    myBook.PublishDate = DateTime.Now;

    First let's create our converter. It's a class that implements System.Windows.Data.IValueConverter (using VisualStudio you can easily generate the structure of the class).

    public class DateTimeConverter : System.Windows.Data.IValueConverter
        {
            public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
            {
                return ( ( DateTime )value ).ToString( "dd MMM, yyyy" );
            }
     
            public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )...
        }

    We have a method Convert, which takes several parameters. The value parameter will be the value of the property that we want to convert. It's of type object so we cast it to DateTime and use the ToString method of the DateTime class to format it as we please. Let's now see how to use this convertor in the Xaml. First declare the namespace of the converter:

    <UserControl x:Class="LayoutExperiments.Page"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:LayoutExperiments">

    My DateTimeConverter class is defined in the Page.cs file, but you can put anywhere you want. The next step is to add the Resource to the UserControl of type DateTimeConverter:

    <UserControl.Resources>
        <local:DateTimeConverter x:Key="DateTimeConverter" />
    </UserControl.Resources>

    And the last thing is to configure the binding and add the converter to it:

    <TextBlock x:Name="MyText" Text="{Binding PublishDate, Converter={StaticResource DateTimeConverter}}"></TextBlock>

    This can also be done entirely in C# using the System.Windows.Data.Binding class:

    TextBlock myText = new TextBlock();
     
    Book myBook = new Book();
    myBook.PublishDate = DateTime.Now;
     
    Binding binding = new Binding("PublishDate");
    binding.Source = myBook;
    binding.Converter = new DateTimeConverter();
     
    myText.SetBinding( TextBlock.TextProperty, binding );
    LayoutRoot.Children.Add( myText );

    So thanks to this converter the value of the TextBlock will be "09 Sep, 2008" instead of "9/9/2008 3:31:PM".

    That's it!




Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)