绑定多个文本块属性

Binding with multiple textblock properties

我有一个要求,当验证保存按钮时,文本块应该变成红色、粗体、下划线并且字体应该变大。

下面是我的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>

Converter code:i 已经为 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();
        }

    }

它的工作方式完全符合我的需要,但我需要一些关于是否可以用更好的方式完成的建议?我们有什么方法可以简化这个

您可以使用 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();
        }

    }