问题
I have created a usercontrol which accept type of enum and assign the values of that enum to a ComboBox control in that usercontrol. Very Simple. I am using this user control in DataTemplates. Problem comes when there comes nested type. I assign that using this notation
EnumType="{x:Type myNamespace:ParentType + NestedType}"
It works fine at runtime. but at design time it throws error saying
Could not create an instance of type 'TypeExtension'
Why? Due to this I am not able to see my window at design time. Any help?
回答1:
According to Rob Relyea form Microsoft this is a defect within the VS2008/2010 designer.
We had somebody look at the use of {x:Type Foo+Bar} pattern and test it in VS2010 and Blend4. It appears that it works fine at Runtime, CompileTime, in Blend 4, but fails in VS2010's WPF Designer.
We've filed a bug, and routed it to the WPF Designer team.
回答2:
According to this post You need to make use of the capabilities the MarkupExtension
class provides.
however there is another way around it:
<ObjectDataProvider
MethodName="GetValues"
ObjectType="{x:Type sys:Enum}"
x:Key="NestedEnumDataProvider">
<ObjectDataProvider.MethodParameters>
<x:Type TypeName="myNamespace:ParentType+NestedType"/>
</ObjectDataProvider.MethodParameters>
</ObjectDataProvider>
来源:https://stackoverflow.com/questions/2278925/design-time-error-while-writing-nested-type-in-xaml