TypeConverter for generic type used in xaml

I am looking for initialization of members of generic types declared in XAML. This is aimed at (reduced) generic support in WPF 4 and the future of Silverlight. (I tried the script below using x:TypeArgumentsand XamlReader.Loadin VS2010 Beta 2, but will be used TestClassInt32 : TestClass<int> { }for simplicity, since it has the same behavior as the use of a common type directly, but it is easier to test using the compiled xaml today.)


These are the types of tests that I use.

public class TestClass<T> {
  [TypeConverter( typeof(StringListToItemsConverter) )]
  public IEnumerable<T> Items { get; set; }
  public TestProperty<T> Property { get; set; }
}

[TypeConverter( typeof(TestPropertyConverter) )]
public struct TestProperty<T> {
  public TestProperty( T value ) : this() { Value = value; }
  public T Value { get; }
}

Here is an example script.

<StackPanel>
  <StackPanel.DataContext>
    <test:TestClassInt32 Items="1,2,3" Property="6" />
  </StackPanel.DataContext>

  <TextBox Text="{Binding Property.Value}" />
  <ItemsControl ItemsSource="{Binding Items}" />
</StackPanel>

typeof(int) , , TestClass<double> TestClass<DateTime>. , TypeConverter.ConvertFrom , . ( , TypeConverter .NET 1.0, , .)


, , :

  • ; [TypeConverter( typeof(TestPropertyConverter<T>) )]

, "" :

  • , ; "sys:Double 1,2,3"
    • Silverlight ( WPF IXamlTypeResolver , "sys:Double")
    • , ; "{List Type={x:Type sys:Double}, Values=1,2,3}"
    • Silverlight ( x:Type, - 1)
    • -, object,
    • , ( , , - Silverlight, , .., .., ).

WPF 4.

+3
2

.NET 4 IServiceProvider, IDestinationTypeProvider, , . .NET 3 4 IProvideValueTarget targetObject targetProperty. targetProperty (a PropertyInfo, MethodInfo -for attach DependencyProperty) .

IDestinationTypeProvider ConvertFrom TypeConverter, :

public override object ConvertFrom(
    ITypeDescriptorContext context,
    CultureInfo culture,
    object value )
{
    var typeProvider =
      (IDestinationTypeProvider)context.GetService( typeof( IDestinationTypeProvider ) );
    Type targetType = typeProvider.GetDestinationType();

    // ... do stuff

    return base.ConvertFrom( context, culture, value );
}
+4

Rob Relyaa , , , , XAML, ITypeDescriptorContext. , System.Xaml. , System.Xaml.

TypeConverter , , TypeDescriptor. (, XAML) , , :

TypeDescriptor.AddAttributes(
  typeof( SomeGenericType<> ),
  new TypeConverterAttribute( typeof( SomeGenericTypeConverter ) ) );

a RedirectTypeConverter . , :

protected RedirectTypeConverter( Type type )
{
    _type = type;
}

// Other methods are implemented similarly.
public override object ConvertFrom(
    ITypeDescriptorContext context,
    CultureInfo culture,
    object value )
{
    InitializeConverter();
    return _converter.ConvertFrom( context, culture, value );
}

public void InitializeConverter()
{
    if ( _converter != null )
    {
        return;
    }

    _converter = TypeDescriptor.GetConverter( _type );
    if ( _converter.GetType() == GetType() )
    {
        string message = string.Format(
          "Conversion failed. Converter for {0} is missing in TypeDescriptor.", _type );
        throw new InvalidOperationException( message );
    }
}

A .

+1

Source: https://habr.com/ru/post/1722709/


All Articles