Combo bound to enum not holding value correctly


Author
Message
Andria Jensen
Andria Jensen
StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)
Group: Forum Members
Posts: 336, Visits: 497
OK, i may try it that way.  Thanks for the help!  This one's been driving me up the wall lately.  w00t
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Yes, you would have to inherit one for each enum.  But, you could also create a generic class so you don't have to duplicate the code every time.

SF.DevExpress.CombEdit -> GenericEnumComboEdit -> SpecificEnumComboEdit

The definition of the GenericEnumComboEdit would be like this:

Public Class GenericEnumComboEdit(Of TEnum)
    Inherits ComboEdit

Everywhere in the code sample before where you have MyEnum, you would replace it with TEnum.

Then, you could have one code file for all of your specific enum combo boxes, because it would only take 3 lines of code for each:

Public Class MyEnumComboEdit
    Inherits GenericEnumComboEdit(Of MyEnum)
End Class

Public Class MyEnum2ComboEdit
    Inherits GenericEnumComboEdit(Of MyEnum2)
End Class

So, it wouldn't require that much code to do it that way if you wanted to.

Andria Jensen
Andria Jensen
StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)
Group: Forum Members
Posts: 336, Visits: 497
So would I have to inherit one for every different enum value then??  Sounds like it would be easier to just have it come back as an integer and cast it when necessary.
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
The problem has to do with the way .NET binding works... if the .NET binding formatting cannot convert the value between the two types, the value is just not copied back to the data source, so, for some reason, .NET cannot convert between the enum and the integer value of the bound property on the combo (maybe because it requires and explicit cast, I dunno).  But we've noticed the same thing... if you have a strong-typed property mapped as an enum, you have to bind to a value on the combo box that is an enum (such as when you use the enum population options of the combo box).  So, if you want to keep your SelectedIndex binding and use an enum, you'll need to inherit the SF DevExpress ComboEdit (or whatever control you're using) and create a custom property that returns an enum for binding.  Like this:

Public Property SelectedIndexEnum() As MyEnum
    Get
        Return CType(Me.SelectedIndex, MyEnum)
    End Get
    Set(ByVal value As MyEnum)
        Me.SelectedIndex = CType(value, Integer)
    End Set
End Property

And you'll also need to create an event for change notification for the binding or all is for nothing Wink

Public Event SelectedIndexEnumChanged As EventHandler

Protected Overrides Sub SelectedIndexChanged(ByVal e As EventArgs)
    '-- Raise the SelectedIndexChanged event
    MyBase.OnSelectedIndexChanged(e)

    '-- Raise your event right after it
    RaiseEvent SelectedIndexEnumChanged(Me, e)
End Sub

So, any time the selected index changes, your custom event "changes," too, so the binding can update the value.

Andria Jensen
Andria Jensen
StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)
Group: Forum Members
Posts: 336, Visits: 497
Just a bit more detail here...this is an inherited DevEx combo.  Also, I can clear the customization in the BOM so that it is just giving me back an integer instead of an enum and it works as expected.  I only get the problem when I have an enum in the BOM customization for the BindingField. 

I know I have posted this before but I never got a fix for it.  I ended up just not using enums in the BOM anymore.  I really would like to resolve this so I can use them without errors like this cropping up.  Thanks for the help.

Andria Jensen
Andria Jensen
StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)StrataFrame User (406 reputation)
Group: Forum Members
Posts: 336, Visits: 497
I have a combo that is populated at runtime with string values and must be done this way due to multilingual issues.  It has a BindingField set to a field which is customized as an enum value, and the BindingProperty set to SelectedIndex.  I figured that this would work since an enum should cast to an integer which would be correct for setting the SelectedIndex property.  What I'm seeing at runtime is that I can change the value in the combobox all I want, but as soon as it loses focuses it goes back to the original value that it was bound to.  So basically, I can't change the value from whatever the original is.  Any reason you know of this is happening, and anything I can do to fix it?
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search