Is there any way to instantiate a 'Type' i

2019-08-10 22:30发布

问题:

It's well known that Silverlight lacks the very compelling x:Type MarkupExtension (MarkupExtension is not supported in Silverlight at all). Is there any dynamic workaround for it?

What about enums (x:Static)?

My need is to have a CommandParameter set to a Type or Enum value, neither of these are supported in Silverlight!

回答1:

This generally has to be done in the code-behind. Even if you build a custom object that exposes a property of type Type, it will not get properly "converted" when set via XAML.

This extra limitation prevents you from creating things like a custom EnumValueProvider, which exposes a Type property and updates a Values properties with the enum values from the specified enumeration type. Having a class like this would allow you to bind a ComboBox or ListBox to a list of enumeration values completely in XAML.

You can create a custom TypeConverter for the above Type property, to somewhat work around this issue. But you don't get the xmlns resolution that you'd expect. But depending on your situation, this may be good enough.

Here is another example, that exposes "known" types via properties, which you can then bind to.