Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/308.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在组合字符串中将StaticResource的值传递给ConverterParameter_C#_Wpf_Ivalueconverter_Staticresource - Fatal编程技术网

C# 在组合字符串中将StaticResource的值传递给ConverterParameter

C# 在组合字符串中将StaticResource的值传递给ConverterParameter,c#,wpf,ivalueconverter,staticresource,C#,Wpf,Ivalueconverter,Staticresource,我写了一个转换器 ValueasBool 参数作为字符串 我是这样使用它的: BorderBrush="{Binding IsSelected, Converter={StaticResource BoolToColorBrushConverter}, ConverterParameter='#ff00bfff;#0000bfff'}" <Color x:Key="MyColor1">#66

我写了一个转换器

  • Value
    as
    Bool
  • 参数
    作为
    字符串
我是这样使用它的:

BorderBrush="{Binding IsSelected,
                      Converter={StaticResource BoolToColorBrushConverter},
                      ConverterParameter='#ff00bfff;#0000bfff'}"
<Color x:Key="MyColor1">#66bb66</Color>

--------------------

BorderBrush="{Binding IsSelected,
                      Converter={StaticResource BoolToColorBrushConverter},
                      ConverterParameter=#ff00bfff;{StaticResource MyColor1}}"
Parameter: "#ff00bfff;#66bb66"
如果
则转换器从参数中的第一个颜色十六进制代码返回一个
彩色画笔
,否则从第二个颜色十六进制代码返回一个
彩色画笔

我的转换器工作得很好,但是我想知道如何像这样使用它:

BorderBrush="{Binding IsSelected,
                      Converter={StaticResource BoolToColorBrushConverter},
                      ConverterParameter='#ff00bfff;#0000bfff'}"
<Color x:Key="MyColor1">#66bb66</Color>

--------------------

BorderBrush="{Binding IsSelected,
                      Converter={StaticResource BoolToColorBrushConverter},
                      ConverterParameter=#ff00bfff;{StaticResource MyColor1}}"
Parameter: "#ff00bfff;#66bb66"
我的问题是如何将组合字符串中的
StaticResource
值传递给我的
ConverterParameter


您的解决方案是什么?

我知道有点晚了,但希望这能帮助已故的访客:

以下是转换器代码:

public class BoolToBorderBrushConverter : IValueConverter
{
    public SolidColorBrush TrueColor { get; set; }

    public SolidColorBrush FalseColor { get; set; }

    // this example compares a binding property (string) with 1 parameter (also in string)
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value != null && parameter != null)
        {
            if (String.Compare(value.ToString(), parameter.ToString(), true) == 0)
            {
                return this.TrueColor;
            }
            else
            {
                return this.FalseColor;
            }
        }
        return this.FalseColor;
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
然后,您可以在xaml中如下配置转换器(在ResourceDictionary部分):


以下是使用转换器的方法:

<Border BorderBrush="{Binding MyProperty, Converter={StaticResource BrushConverter}, ConverterParameter=ABC}"/>


将整个参数字符串设置为资源,或将属性添加到转换器中,创建具有不同属性值的多个转换器对象,或将多重绑定与IMultiValueConverter一起使用。您不能这样做。相反,请添加一个颜色为的属性,您可以使用该属性绑定并从中调用转换器。这看起来是
多值转换器的完美任务。
。谢谢大家,@Clemens,请给我3个非常简单的示例代码(包括转换器方法的签名,只需要XAML中的代码行来描述如何使用它)答案中的3个解决方案中的每一个?