gpt4 book ai didi

c# - 将 StaticResource 的值传递给组合字符串中的 ConverterParameter

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:42 25 4
gpt4 key购买 nike

我写了一个转换器来获取

  • Value 作为 Bool
  • 参数作为字符串

我是这样使用的:

BorderBrush="{Binding IsSelected,
Converter={StaticResource BoolToColorBrushConverter},
ConverterParameter='#ff00bfff;#0000bfff'}"

如果 ValueTrue,则转换器从参数中的第一个颜色十六进制代码返回一个 ColorBrush,否则返回一个 ColorBrush 来自第二种颜色的十六进制代码。

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

<Color x:Key="MyColor1">#66bb66</Color>

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

BorderBrush="{Binding IsSelected,
Converter={StaticResource BoolToColorBrushConverter},
ConverterParameter=#ff00bfff;{StaticResource MyColor1}}"

设计模式下的结果:

enter image description here

运行时结果:

enter image description here

但是我的参数中需要 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}"/>

关于c# - 将 StaticResource 的值传递给组合字符串中的 ConverterParameter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30935939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com