提问者:小点点

C#WPF绑定转换器如何为函数指定参数


在我的窗口。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}}"/> 

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

我的ColorConverter.cs帮助器目录中的类:

    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中确定颜色是橙色还是黄色。有什么好方法可以做到这一点吗?


共3个答案

匿名用户

如果要向转换器提供附加值,可以:

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

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

b) 利用CommandParameter,它通常是字符串文字:

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

匿名用户

如果要绑定多个值,请使用多值转换器

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

然后使用多重绑定并指定要绑定的属性以及笔刷。

<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并在转换器中强制转换“parameter”参数:

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

相关问题