我想使用布尔值绑定 FontAwesome/MaterialFont 图标和颜色。我已成功绑定字体图标,但无法绑定颜色。
isSQL 是布尔值
代码...
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is bool boolValue)
{
string trueFontIcon = "\U000f05e0";
string falseFontIcon = "\U000f05d6";
Color trueColor = Color.Green;
Color falseColor = Color.Red;
return boolValue ? (trueFontIcon,trueColor) : (falseFontIcon, falseColor);
}
return ("\U000f1522", Color.Gray);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return value;
}
XAML...
<ResourceDictionary>
<local:BooleanToFontIconConverter x:Key="BooleanToFontIconConverter" />
</ResourceDictionary>
<Label Grid.Column="1" tooltip:TooltipEffect.Text="{Binding sqlMessage}" FontFamily="MaterialFont">
<Binding Path="isSQL" Converter="{StaticResource BooleanToFontIconConverter}" />
</Label>
如果您想使用一个转换器更改文本和文本颜色,您可以尝试使用ConverterParameter。
当您使用绑定时,
<Label Grid.Column="1" tooltip:TooltipEffect.Text="{Binding sqlMessage}" FontFamily="MaterialFont"
Text="{Binding isSQL,Converter={StaticResource BooleanToFontIconConverter},ConverterParameter="Text"}"
TextColor="{Binding isSQL,Converter={StaticResource BooleanToFontIconConverter},ConverterParameter="TextColor"}">
</Label>
所以在BooleanToFontIconConverter中,你可以根据传入的ConverterParameter决定返回FontIcon或TextColor,
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
string param = parameter as string;
if (value is bool boolValue)
{
string trueFontIcon = "\U000f05e0";
string falseFontIcon = "\U000f05d6";
Color trueColor = Color.Green;
Color falseColor = Color.Red;
if(param == "Text")
{
return boolValue ? trueFontIcon : falseFontIcon;
}
else
{
return boolValue ? trueColor : falseColor;
}
}
...
现在您可以同时更改 TextColor 和 FontIcon。
希望有帮助!