1

I'm trying to get a type object in XAML.

x:Type={...}

But I realized that Silverlight does not support it. I was trying to use it in the following question (in EnumerationExtension class):

Databinding an enum property to a ComboBox in WPF

What can I do to pass the type in XAML?C

Community
  • 1
  • 1
Darf Zon
  • 6,268
  • 20
  • 90
  • 149

2 Answers2

1

if SL 5 then custom MarkupExtension
else Binding with Converter that returns value.GetType()


ME example:

public class TypeExtension : IMarkupExtension<Type>
{
    public string TypeName { get; set; }

    public TypeExtension() { }
    public TypeExtension(string typeName)
        : this()
    {
        if (typeName == null) throw new ArgumentNullException("typeName");

        TypeName = typeName;
    }

    public Type ProvideValue(IServiceProvider serviceProvider)
    {
        var typeResolver = (IXamlTypeResolver)serviceProvider.GetService(typeof(IXamlTypeResolver));
        var type = typeResolver.Resolve(TypeName);
        return type;
    }
}

Note that there is no support for the constructors in SL 5, so you need to use the property names in XAML:

{me:Type TypeName=local:SomeClass}
H.B.
  • 166,899
  • 29
  • 327
  • 400
  • I'm using SL5. I have a doubt, so have I create a MyType custom markup extension and set the PrivideValue the object.GetType right? – Darf Zon Aug 21 '12 at 21:19
0

You could create a Custom Markup Extension (available since Silverlight 5)

Or you could use this one

Emond
  • 50,210
  • 11
  • 84
  • 115