(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

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

(8 votes)
Martin Mihaylov
>
Martin Mihaylov
Joined Oct 29, 2007
Articles:   50
Comments:   70
More Articles
6 comments   /   posted on Sep 09, 2008
Categories:   Data 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!


Subscribe

Comments

  • -_-

    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...
  • Enrai

    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

  • andrew.veresov

    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!
  • -_-

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


    posted by serhio on Dec 10, 2010 15:35
    this is cool. Now pass me please the value "1/3" as double parameter ;)
  • Enrai

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


    posted by Enrai on Dec 10, 2010 15:58

    Hello!

     The only thing that I can come up with is to set the fraction 1/3 as a string "1/3" to the ConverterParameter. After that in the converter you can easily parse the string to the desired double value. Hope that helps!

    Greets!

Add Comment

Login to comment:
  *      *