【问题标题】:Binding Converter how to pass a parameter to function绑定转换器如何将参数传递给函数
【发布时间】:2022-01-19 16:23:30
【问题描述】:

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

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

我在 Helpers 目录中的 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 中确定颜色是橙色还是黄色。有什么好的方法可以做到吗?

【问题讨论】:

    标签: c# wpf xaml data-binding converters


    【解决方案1】:

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

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

    【讨论】:

    • 绑定的不是CommandParameter,而是ConverterParameter
    • 而且它不是“通常是字符串文字”,而是任何object
    • @mm8 是的,任何对象,但在大多数情况下,这最终将是一个字符串或数字,除非您喜欢在 XAML 中创建对象,因为您不能在转换器参数中使用绑定:)
    • @thatguy 感谢您了解这一点
    【解决方案2】:

    如果要绑定多个值,请使用MultiValueConverter。我在这里使用更通用的类型Brush,所以你不限于绑定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 并指定要绑定的属性以及画笔。为了引用内置画笔,您可以使用静态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>
    

    【讨论】:

      【解决方案3】:

      您可以将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}}"/>
      

      【讨论】:

        猜你喜欢
        • 2014-01-26
        • 2011-08-07
        • 1970-01-01
        • 2019-09-10
        • 2021-02-14
        • 1970-01-01
        • 2013-10-31
        • 2022-07-16
        相关资源
        最近更新 更多