C# 无法使转换器工作

C# 无法使转换器工作,c#,wpf,binding,ivalueconverter,C#,Wpf,Binding,Ivalueconverter,我正在努力学习如何使用IValueConverter。我有以下转换器: [ValueConversion(typeof(string), typeof(string))] public class RequiredFieldConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, CultureInfo culture) {

我正在努力学习如何使用IValueConverter。我有以下转换器:

[ValueConversion(typeof(string), typeof(string))]
public class RequiredFieldConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return "";

        return value.ToString() + "*";
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (value == null)
            return "";
        var str = value.ToString();
        return str+"Convert Back testing";
    }
}
我已将RequiredFieldConverter资源添加到我的app.xaml文件中,我想尝试以下方式:

<TextBox Name="textBox2"  Width="120" />
<TextBox Text="{Binding ElementName=textBox2, Path=Text, Converter=RequiredFieldConverter}" Name="textBox3" Width="120" />

。。。当它试图将
RequiredFieldConverter
解释为对
IValueConverter
引用时,会出现错误。您需要使用
StaticResource
DynamicResource
引用转换器,就像您在第二个示例中所做的那样

<TextBox Text="{Binding ElementName=textBox2, Path=Text, Converter={StaticResouce RequiredFieldConverter}}" Name="textBox3" Width="120" />

。。。当它试图将
RequiredFieldConverter
解释为对
IValueConverter
引用时,会出现错误。您需要使用
StaticResource
DynamicResource
引用转换器,就像您在第二个示例中所做的那样

<TextBox Text="{Binding ElementName=textBox2, Path=Text, Converter={StaticResouce RequiredFieldConverter}}" Name="textBox3" Width="120" />