Wpf Can';t使用TypeConverter将资源转换为字符串
例外情况: 无法将属性“Text”中的值转换为类型为的对象 “System.String”。无法强制转换对象 类型为“MyApp.Foo”的 “System.String” XAML:Wpf Can';t使用TypeConverter将资源转换为字符串,wpf,resources,typeconverter,Wpf,Resources,Typeconverter,例外情况: 无法将属性“Text”中的值转换为类型为的对象 “System.String”。无法强制转换对象 类型为“MyApp.Foo”的 “System.String” XAML: 怎么了?您不需要使用值转换器而不是类型转换器吗 XAML 你不需要使用值转换器而不是类型转换器吗 XAML 这是一个正确的方向。它使用Text=“{Binding Converter={StaticResource fooConverter},Source={StaticResource foo},Mode=On
怎么了?您不需要使用值转换器而不是类型转换器吗 XAML
你不需要使用值转换器而不是类型转换器吗 XAML
这是一个正确的方向。它使用Text=“{Binding Converter={StaticResource fooConverter},Source={StaticResource foo},Mode=OneWay}”如何在没有XAML的情况下获得相同的结果?这是一个正确的方向。它使用Text=“{Binding Converter={StaticResource fooConverter},Source={StaticResource foo},Mode=OneWay}”如何在没有XAML的情况下获得相同的结果?
<Window.Resources>
<my:Foo x:Key="foo"></my:Foo>
</Window.Resources>
<TextBox Text="{DynamicResource foo}"></TextBox>
[TypeConverter(typeof(FooConverter))]
public class Foo
{
public Foo()
{}
}
public class FooConverter : TypeConverter
{
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return true;
}
public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
{
return "Foo";
}
}
<Window.Resources>
<my:Foo x:Key="foo"/>
<my:FooConverter x:Key="fooConverter />
</Window.Resources>
<TextBox Text="{DynamicResource foo, Converter={DynamicResource fooConverter}}"></TextBox>
public class FooConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((Foo)value).ToString();
}
}