绑定转换器如何将参数传递给函数

Binding Converter how to pass a parameter to function

在我的 window.xaml 中,我有以下代码:

 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}}"/> 

我还想在字符串“Yellow”或“Orange”中给该函数一个值,这样我就可以对具有不同颜色的不同矩形使用相同的函数。

我的ColorConverter.csclass在Helpers目录里面:

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();
    }
}

这样我就可以在 XAML 中确定颜色必须是橙色还是黄色。请问有什么好的方法吗?

当您想为转换器提供附加值时,您可以:

a) 在转换器上添加额外的属性,然后可以在 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();
    }
}
...
<converters:ColorConverter x:Key="ColorOnChange" BackupColor="Yellow"/>
...

b) 利用 the ConverterParameter 通常是字符串文字:

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

如果要绑定多个值,请使用 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();
   }
}

然后使用 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 类型 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>

您可以将 CommandParameter 属性 设置为 BrushColor 并在转换器中转换“参数”参数:

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}}"/>