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

Parents
  • 54937
    Offline posted

    I"m guessing its because the TypeConverter for System.Enum does not know how to convert from string to System.Enum which is all the context that the WPF binding infrastructure has available. I'm suspect this has nothing to do with our controls and you would see this same thing if you bound the Title property of the Window or the Text property of a TextBox as you are binding the Text property of the XamTextEditor. I think you probably need to create your own IValueConverter that will handle the String<=>MyEnum conversions and provide that as the converter on the binding you are creating.

Reply Children
No Data