(X) Hide this Upcoming webinar on Feb 23rd, 10 am PST (see your local time): Building Line-of-business Applications with Silverlight & WCF Data Services
More webinar info | Register | Other webinars
Tweet @silverlightshow and win a SilverlightShow Tweet-shirt. Learn how >>
Skip Navigation LinksHome / Search

Search

 
Results Per Page

Found 5 results for Value Converter.
Date between: <not defined> and <not defined>
Search in: News , Articles , Tips , Shows , Showcase , Books

Order by Publish Date   Ascending Title   Rating  

  • 0 comments  /  posted by  Silverlight Show  on  Nov 17, 2010 (3 months ago)
    Tags: ValueConverter , MVVM , Kunal Chowdhury

    SilverlightShow Page for all Silverlight and Windows Phone 7 (WP7) things on TwitterKunal Chowdhury explains to one of his readers how to show a TextBox on selection of “Others” from a ComboBox using a ValueConverter.

    Source: Kunal's Blog

    One of my reader “Girish” recently contacted me with a problem statement. His problem is, he wants to show a TextBox on selection of “Others” from a ComboBox and I informed him to use a ValueConverter and also told him, I will do a blog post on it, so that he can understand each step from that.

    This post I am dedicating to him. May be, some other people who are new to Silverlight can get benefit from it. Read the full post to learn the process to do it.



  • Value Converters and Extension Methods in Silverlight Binding

    0 comments  /  posted by  Silverlight Show  on  Mar 09, 2009 (more than a year ago)

    Pete Brown will show you some options of Value Converters and Extension Methods in Silverlight Binding.

    I love binding in Silverlight, but sometimes you need to do things with binding that you know how you would do in inline code, but maybe aren’t sure how to handle in binding. Nine times out of ten, a value converter can help you out.

  • 0 comments  /  posted by  Silverlight Show  on  Feb 04, 2009 (more than a year ago)
    Tags: ChangeableObject , ValueConverter , Silverlight , WPF

    Nick on Silverlight and WPF has posted a couple more bits of code about ChangeableObject and ValueConverter  that others might find useful.

    Nick:
    Whenever I do databinding-intensive apps (both Silverlight & WPF), I find myself writing a fair amount of boilerplate code.  If you want change notifications on your class, you need to inherit from INotifyPropertyChanged, define a PropertyChanged event, and write a couple lines of code to fire it.
  • 6 comments  /  posted by  Martin Mihaylov  on  Sep 09, 2008 (more than a year ago)

    If you're not familiar with the value converters read this. The methods generated by the VisualStudio when creating a custom class that implements System.Windows.Data.IValueConverter have several arguments. One of them is of type object and is called parameter.

    public class DateTimeConverter : System.Windows.Data.IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )...
       
        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )...
    }

    In this example we bind to an object of type Book:

    public class Book
    {
        public DateTime PublishDate { get; set; }
    }

    We can pass this argument from the code or from the Xaml:

    Xaml

    <TextBlock Text="{Binding PublishDate, Converter={StaticResource DateTimeConverter}, ConverterParameter=true}"/>

    C#

    Book myBook = new Book();
    myBook.PublishDate = DateTime.Now;
     
    Binding binding = new Binding( "PublishDate" );
    binding.Source = myBook;
    binding.Converter = new DateTimeConverter();
    binding.ConverterParameter = true;

    That's it!

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

    First let's explain what the converters 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 converter 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!