如何在blazor中使用TypeConverter进行绑定

我正在尝试在blazor中使用转换器;

I already sew typeconverters in wpf https://www.codeproject.com/Articles/443537/Using-a-TypeConverter-for-Binding-in-WPF I'm trying do the same but in blazor

public class ConvertBoolToYesOrNo : IValueConverter
{

    public ConvertBoolToYesOrNo()
    {

    }
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (bool.Parse(value.ToString()))
            return "Yes";
        return "No";
    }
    //If Convert Fail
    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return "The converter doen't converted the value";
    }
}

======== Test.Razor ==========

@{
     Value = "true";
 }

    <label>@Value</label>

@code{
    [TypeConverterAttribute(typeof(ConvertBoolToYesOrNo))]
    public string Value{ get; set; }
}
输出为真   预期是
评论