none
Binding and "converterparameter" RRS feed

  • Question

  • Hi,

    I have a class called Graph and it has dependency properties - "Value" and "MaxRange". I have written one converter class and my target is to send "Value" as a value and "MaxRange" as parameter to the Convert() method to compare.

    In the DataTrigger using binding I can send "Value" as a value but I am not sure how to send "MaxRange" as a parameter using "converterparameter"..

    Is there any way to achieve this??

    Here is a xaml code -

    <Style TargetType="{x:Type Input:Graph}">
            <Setter Property="Width" Value="20.000"/>
            <Setter Property="Height" Value="115.500"/>
            <Setter Property="Template" Value="{StaticResource WithinRange_ControlTemplate}"/>
            <Style.Resources>
                <Input:RangeConverter x:Key="RangeConverter"/>
            </Style.Resources>
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=Value, RelativeSource={RelativeSource Self}, Converter={StaticResource RangeConverter}, ConverterParameter=??????}" Value="true">
                    <Setter Property="Template" Value="{StaticResource BelowRange_ControlTempalte}"/>
                </DataTrigger>              
            </Style.Triggers>
        </Style>
    </ResourceDictionary>

    Thanks in Advance,

    Prasad.


    • Edited by PrasadMSDN Friday, November 29, 2013 7:18 AM
    Friday, November 29, 2013 7:17 AM

Answers

  • Hi,

    you can use MultiBinding in combination with MultiValueConverter to achieve your goal.

    <MultiBinding Converter="{StaticResource RangeConverter}">
      <Binding Path="Value" RelativeSource="{RelativeSource Self}" />
      <Binding Path="MaxRange" />
    </MultiBinding>
    public class RangeConverter : IMultiValueConverter
    {
      public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
        object value = values[0];
        object maxRange = values[1];
        // ...
      }
    
      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
      {
        // ...
      }
    }

    Best regards




    Chris

    Code Samples: Code Samples
    Chrigas Blog: Chrigas Blog

    • Marked as answer by PrasadMSDN Monday, December 2, 2013 4:02 AM
    Friday, November 29, 2013 10:53 AM

All replies

  • Hi Prasad,

    The answer is you can't bind any value to the converter parameter directly. You need to implement a workaround.

    Please refer this post

    http://www.codeproject.com/Articles/456589/Bindable-Converter-Parameter


    srithar

    • Proposed as answer by Jason Dot Wang Tuesday, December 3, 2013 3:23 AM
    Friday, November 29, 2013 9:16 AM
  • Hi,

    you can use MultiBinding in combination with MultiValueConverter to achieve your goal.

    <MultiBinding Converter="{StaticResource RangeConverter}">
      <Binding Path="Value" RelativeSource="{RelativeSource Self}" />
      <Binding Path="MaxRange" />
    </MultiBinding>
    public class RangeConverter : IMultiValueConverter
    {
      public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
      {
        object value = values[0];
        object maxRange = values[1];
        // ...
      }
    
      public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
      {
        // ...
      }
    }

    Best regards




    Chris

    Code Samples: Code Samples
    Chrigas Blog: Chrigas Blog

    • Marked as answer by PrasadMSDN Monday, December 2, 2013 4:02 AM
    Friday, November 29, 2013 10:53 AM
  • Thank you Srithar for your response.. I will also look into the workaround !

    Best Regards,

    Prasad.

    Monday, December 2, 2013 4:04 AM
  • Great ! it worked! Thank you very much Chris ! 

    Best Regards,

    Prasad.


    Monday, December 2, 2013 4:06 AM