Is there any way to instantiate a 'Type' in Silverlight XAML?

。_饼干妹妹 提交于 2020-01-05 10:10:13

问题


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.



来源:https://stackoverflow.com/questions/5622622/is-there-any-way-to-instantiate-a-type-in-silverlight-xaml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!