Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
1540
Exception when binding run-time Enum to XamComboEditor
posted

I have a class with an Enum member which I assign a run-time Enum type as thus:

AppDomain currentDomain = AppDomain.CurrentDomain;
AssemblyName aName = new AssemblyName("EnumAssembly");
AssemblyBuilder ab = currentDomain.DefineDynamicAssembly(aName, AssemblyBuilderAccess.Run);
ModuleBuilder mb = ab.DefineDynamicModule(aName.Name);
EnumBuilder eb = mb.DefineEnum("MyEnum", TypeAttributes.Public, typeof(int));
eb.DefineLiteral("MyEnum1", 0);
eb.DefineLiteral("MyEnum2", 1);
Type myEnumType = eb.CreateType();
_myEnum = (Enum)Activator.CreateInstance(myEnumType);

 

 

_myEnum is defined as a member like this:

public Enum _myEnum;

 

Elsewhere in the same class as this above member, I bind a XamComboEditor to this member like this:

 XamComboEditor cbo = new XamComboEditor();
cbo.ItemsSource = Enum.GetValues(_myEnum.GetType());
cbo.ValueType = _myEnum.GetType(); Binding binding = new Binding("_myEnum");
binding.Source = this;
cbo.Value = _myEnum;
cbo.SetBinding(XamTextEditor.TextProperty, binding); 

 

However, on the last line of code, I get the following error message:

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll

Additional information: Type provided must be an Enum.

 

How do I bind my run-time Enum to a XamComboEditor without causing this error message?

Any help greatly appreciated.

Thanks,

Jason