在保存按钮上进行验证时,我有一个要求文本块应变为红色,粗体,下划线和字体应变大。
下面是我的xamlcode
<TextBlock HorizontalAlignment="Right"
Foreground="{x:Bind Model.FirstNameError, Converter={StaticResource ErrorColorConverter}, Mode=OneWay}"
FontStyle="{x:Bind Model.FirstNameError, Converter={StaticResource ErrorFontStyleConverter}, Mode=OneWay}"
FontSize="{x:Bind Model.FirstNameError, Converter={StaticResource ErrorFontSizeConverter}, Mode=OneWay}"
<Run Text="First Name" TextDecorations="{x:Bind Model.FirstNameError, Converter={StaticResource TextUnderlineConverter},Mode=OneWay}" />
</TextBlock>
转换器代码:我为ErrorColorConverter,ErrorFontSizeConverter和TextUnderlineConverter创建了多个转换器,如下所示:
public class ErrorFontStyleConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if ((bool)value)
return FontStyle.Italic;
else
return FontStyle.Normal;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
答案 0 :(得分:2)
您可以使用ConverterParameter
并从单个转换器接收它们
<TextBlock Foreground="{x:Bind FirstNameError,Mode=OneWay,Converter={StaticResource ErrorToFont},ConverterParameter=foreground}"
FontStyle="{x:Bind FirstNameError,Mode=OneWay,Converter={StaticResource ErrorToFont},ConverterParameter=fontstyle}"
FontWeight="{x:Bind FirstNameError,Mode=OneWay,Converter={StaticResource ErrorToFont},ConverterParameter=fontweight}">
//转换器
public class ErrorFontConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (parameter.ToString() == "fontstyle")
return (bool)value ? Windows.UI.Text.FontStyle.Italic : Windows.UI.Text.FontStyle.Normal;
else if (parameter.ToString() == "foreground")
return (bool)value ? new SolidColorBrush(Colors.Red) : new SolidColorBrush(Colors.Blue);
else
return (bool)value ? FontWeights.Bold : FontWeights.Normal;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}