[英]How to pass a string value from resource file to a ConverterParameter?
[英]Pass value of a StaticResource to ConverterParameter in a combine string
我写了一个转换器,得到
Bool
Value
Parameter
作为String
我像这样使用它:
BorderBrush="{Binding IsSelected,
Converter={StaticResource BoolToColorBrushConverter},
ConverterParameter='#ff00bfff;#0000bfff'}"
如果Value
是True
,则转换器返回ColorBrush
从第一颜色的十六进制代码参数,否则返回一个ColorBrush
从第二颜色的十六进制代码。
我的转换器工作得很好但我想知道如何使用它:
<Color x:Key="MyColor1">#66bb66</Color>
--------------------
BorderBrush="{Binding IsSelected,
Converter={StaticResource BoolToColorBrushConverter},
ConverterParameter=#ff00bfff;{StaticResource MyColor1}}"
设计模式下的结果:
运行时结果:
但是我需要像这样在我的参数中使用StaticResource
颜色十六进制代码:
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 部分):
<ResourceDictionary>
<local:BoolToBorderBrushConverter x:Key="BrushConverter" TrueColor="{StaticResource MyTrueColor}" FalseColor="Transparent">
</ResourceDictionary>
这就是您使用转换器的方式:
<Border BorderBrush="{Binding MyProperty, Converter={StaticResource BrushConverter}, ConverterParameter=ABC}"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.