Home > database >  Binding Converter how to pass a parameter to function
Binding Converter how to pass a parameter to function

Time:12-17

In my window.xaml I have the following code:

 xmlns:converters="clr-namespace:HMIPlc.Helpers"

 <Window.Resources>
    <ResourceDictionary>
        <converters:ColorConverter x:Key="ColorOnChange"/>
    </ResourceDictionary>
</Window.Resources>

<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange}}"/> 

I want to give also a value in a string "Yellow" or "Orange" to the function, so I can use the same function for different rectangles with different colors.

My ColorConverter.cs class inside the Helpers directory:

public class ColorConverter : IValueConverter
{
    public ColorConverter()
    {
    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool tempBool = (bool)value;
        if(tempBool == true)
        {
            return new SolidColorBrush(Colors.Orange);
        } else
        {
            return new SolidColorBrush(Colors.White);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

So that I can determine in my XAML if the color has to be orange or yellow. Is there any good method to do this?

CodePudding user response:

When you want to provide additional values to a converter, you can either:

a) Add additional properties on the converter which can then be assigned in XAML:

public class ColorConverter : IValueConverter
{
    public Color BackupColor { get; set; }

    public ColorConverter()
    {

    }

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool tempBool = (bool)value;
        if(tempBool == true)
        {
            return new SolidColorBrush(Colors.Orange);
        } else
        {
            return new SolidColorBrush(Colors.White);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}```

```xaml
...
<converters:ColorConverter x:Key="ColorOnChange" BackupColor="Yellow"/>
...

b) Leverage the CommandParameter which is usually a string literal:

<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange}, CommandParameter='Yellow'}"/>

CodePudding user response:

If you want to bind multiple values, use a MultiValueConverter. I use the more general type Brush here, so you are not limited to binding a SolidColorBrush.

public class ColorConverter : IMultiValueConverter
{
   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
   {
      if (values.Length != 3 ||
          !(values[0] is bool value) ||
          !(values[1] is Brush brushTrue) ||
          !(values[2] is Brush brushFalse))
         return Binding.DoNothing;

      return value ? brushTrue : brushFalse;
   }

   public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
   {
      throw new NotImplementedException();
   }
}

Then use a MultiBinding and specify the property to be bound, as well as the brushes. In order to reference a built-in brush, you can use the static Brushes type with x:Static.

<Rectangle>
   <Rectangle.Fill>
      <MultiBinding Converter="{StaticResource ColorOnChange}">
         <Binding Path="varUnit.InSimulation"/>
         <Binding Source="{x:Static Brushes.Orange}"/>
         <Binding Source="{x:Static Brushes.White}"/>
      </MultiBinding>
   </Rectangle.Fill>
</Rectangle>

CodePudding user response:

You could set the CommandParameter property to a Brush or Color and cast the "parameter" argument in your converter:

public class ColorConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        Color color = (Color)parameter;
        bool tempBool = (bool)value;
        if (tempBool == true)
        {
            return new SolidColorBrush(color);
        }
        else
        {
            return new SolidColorBrush(color);
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<Rectangle Fill="{Binding Path=varUnit.InSimulation, Converter={StaticResource ColorOnChange},
        ConverterParameter={x:Static Colors.Orange}}"/>
  • Related