WPF Bind value to Binding ConverterParameter

Sometimes it would be necessary if we could just bind a value to a ConverterParameter.
For example if you need two properties in the IValueConverter. It is possible to add a ConverterParameter but we cannot bind to it.

<!-- The Converter in the Resources -->
<my:MyConverter x:Key="myConverter" />

<!-- This does not work because we can not bind DEF to the ConverterParameter -->
<TextBlock Text="{Binding Path=ABC, Converter={StaticResource myConverter},ConverterParameter={Binding Path=DEF}}" />

<!-- This solution with the MultiValueConverter works -->
<TextBlock>
  <TextBlock.Text>
    <MutliBinding Converter="{StaticResource myConverter}">
      <MultiBinding.Bindings>
        <Binding Path="ABC" />
        <Binding Path="DEF" />
      </MultiBinding.Bindings>
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

The MultiValueConverter should look like this

public class MyConverter: IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (double)values[0] + " " + (double)values[1];
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotSupportedException("Not implemented");
    }
}

The MultiValueConverter is a very powerful feature of WPF which allows us to bind more than one object.

About these ads
  1. Hi MichLG,
    Great post. It works.
    But what if I want to pass a value that is in a TextBox and myDataGrid content?
    I need the textbox to make a filter when validating.

    Is it posible to pass that value?

    Regards, Lina

      • michlG
      • January 28th, 2010

      Hi Lina,
      thank you for you comment.
      What exactly do you want to achieve?
      You can simply bind the value of the TextBox and the value of the DataGrid using the MultiBinding.
      Hm.. why aren’t you using the Converter to filter the data (instead of the validator).
      As far as I know it is not possible to use some kind of a extra Parameter in Validators (which means that you just have the value)..

      Regards
      Michael

      • Because I have a value that the user selected in a textbox. i need to know that value in order to use it in the validator.

        • michlG
        • January 29th, 2010

        I don’t know exactly what you want to achieve, but I is related to Validators and not Converters.
        Maybe you can find some useful information here: codeproject – wpfvalidation

      • Because de hhe Validator is used to validate the value that the user inputs into the DataGrid before the new value is submitted to the data source. The Converter is used in a DataTrigger to change the background of the DataGridCell if the data in the data source is dirty. As you can see, they have two different functionality.

        In my case I need the one to change the color after it is validated.

        Thanks a lot!!

        • michlG
        • January 29th, 2010

        a ok.. well you could just add a trigger to Validator.HasErrors.
        If it is false it means that it is validated.

        Check this example here:
        http://www.thejoyofcode.com/Reason_10._Validation.aspx

        You could even implement your own validator which then would make it a little bit easiert o create this trigger.
        But i don’t know if it is necessary in this case

  2. That is correct. And I have it working that way.
    Now, I need to pass a variable with a number as well as the cell data being validated. my problem y how can I pass that value? I need to know the value of 2 cells in order to validate the first cell. That’s why I was thinking on Multibinding, but that doesn’t work, because it never pasess the value of the variable. It pasess the name itself.

    I do not if there is a workaround.

    • Serg
    • August 23rd, 2010

    Great. Thanks

  3. Another way is making your converter inherit from DependencyObject. This allows you to declare dependency properties, making it possible to set its values from XAML, even a Binding.

    Example: A converter to multiply a value specifing the factor, which is obtained from a property in a custom control.

    - Converter:
    public class MyConverter: DependencyObject : IValueConverter
    {

    // The property used as a parameter
    public double Factor
    {
    get { return (double)GetValue(FactorProperty); }
    set { SetValue(FactorProperty, value); }
    }

    // The dependency property to allow the property to be used from XAML.
    public static readonly DependencyProperty FactorProperty =
    DependencyProperty.Register(
    “Factor”,
    typeof(double),
    typeof(MyConverter),
    new PropertyMetadata(1.15d));

    #region IValueConverter Members

    object IValueConverter.Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    // Use the property in the Convert method instead of “parameter”
    return original * Factor;
    }

    object IValueConverter.ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
    throw new NotImplementedException();
    }
    }

    - XAML

    So, you can now declare a dependency property for each parameter you need in your converter and bind it in Silverlight 4.

      • michlG
      • November 16th, 2011

      Hey,

      thanks for that.
      That is a nice way to solve that issue.

      - Michael

  4. The XAML code i left before: A converter is declared and its Factor property is setted using a Binding.

  5. Well, it seems that you can not post XAML code in comments, so I posted it to stackoverflow:

    http://stackoverflow.com/questions/377841/what-should-the-converter-parameter-be-for-this-binding/8151273#8151273

      • michlG
      • November 17th, 2011

      All rigth. Thanks for that

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: