Skip Navigation LinksHome / Tips / View Tip

Tip: How to pass a parameter to a value converter?

+ Add to SilverlightShow Favorites
4 comments   /   posted by Martin Mihaylov on Sep 09, 2008
(4 votes)
Categories: Binding

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!

Share


Comments

Comments RSS RSS
  • RE: Tip: How to pass a parameter to a value converter?  

    posted by Jeeva on Mar 05, 2010 14:08
    Hi i need to pass a binding value as a parameter to the converter class.. please help me in sorting this...
  • RE: Tip: How to pass a parameter to a value converter?  

    posted by Enrai on Mar 05, 2010 14:21

    It's still not possible to bind the ConverterParameter in Silverlight. But there is a possible workaround:

    If you have a business object with for example two properties - Value and Parameter, and you want to bind to the Value and pass the Parameter to the ConverterParameter. The solution is to bind to the entire business object, so in the Convert() method of the converter you will have access to both the Value and the Parameter properties.

    Hope this helps!

    Greets,
    Martin

  • RE: Tip: How to pass a parameter to a value converter?  

    posted by andrew.veresov on Mar 05, 2010 17:35
    You could modify converter to expect not simple type as a value but well known for the converter interface. Interface could give all info needed for converter. In this case you don't need to bind converter parameter. See here for more info.
  • RE: Tip: How to pass a parameter to a value converter?  

    posted by wpf noob on May 10, 2010 19:06
    Awesome! Couldn't find how to programmatically bind and convert anywhere else... thank you!

Add Comment

 
 

   
  
  
   
Please add 6 and 8 and type the answer here:

Join the free SilverlightShow webcast 'Running Silverlight Outside the Browser and with Elevated Trust'. Sept 7th, 8 am - 9 am PDT.
In this live session Chris Anderson will cover configuring and debugging OOB mode, toast notifications, elevated trust, direct file access and much more.
Learn more | Register | See more webinars (hide this)