C# 无法使转换器工作
我正在努力学习如何使用IValueConverter。我有以下转换器: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) {
[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" />