Click here to Skip to main content
15,894,540 members
Home / Discussions / WPF
   

WPF

 
QuestionSilverlight business application Pin
picasso23-Jun-13 19:19
picasso23-Jun-13 19:19 
AnswerRe: Silverlight business application Pin
Abhinav S10-Jun-13 20:25
Abhinav S10-Jun-13 20:25 
QuestionWPF Combo box not display bound value Pin
Dominick Marciano2-Jun-13 10:10
professionalDominick Marciano2-Jun-13 10:10 
AnswerRe: WPF Combo box not display bound value Pin
AlphaDeltaTheta2-Jun-13 15:43
AlphaDeltaTheta2-Jun-13 15:43 
AnswerRe: WPF Combo box not display bound value Pin
Mycroft Holmes2-Jun-13 20:12
professionalMycroft Holmes2-Jun-13 20:12 
QuestionSuggestions for Email Handling Pin
MehGerbil28-May-13 9:07
MehGerbil28-May-13 9:07 
AnswerRe: Suggestions for Email Handling Pin
Mycroft Holmes28-May-13 14:28
professionalMycroft Holmes28-May-13 14:28 
QuestionBinding Enum to a ComboBox Pin
Kenneth Haugland28-May-13 7:05
mvaKenneth Haugland28-May-13 7:05 
Before I start the actual question I do know about these articles:
Fill Combobox With Sorted Enum Without Code[^]
Binding and Using Friendly Enums in WPF[^]
Funky WPF - Enumerations and Combo Boxes[^]
Enum to ComboBox binding[^]

However what I want to do is the folloing. Assume a Class that has an Enum as a Property:
VB.NET
Class Test
    Public Enum Something As Integer
        <Description("Something happended")> Something
        <Description("Nothing happended")> Nothing_
    End Enum

    Private pEnumer As Something = -1
    Public Property Enumer() As Something
        Get
            Return pEnumer
        End Get
        Set(ByVal value As Something)
            pEnumer = value
        End Set
    End Property
End Class


And now I want to bind a ComboBox to the Property Enumer, and I thought It would be simple to make an own based on the original ComboBox:
VB.NET
Public Class EnumComboBox
        Inherits ComboBox

        Private _EnumList As New List(Of EnumerationMember)

        Private pEnumItemsSorce As [Enum]
        Public Property EnumItemsSorce() As [Enum]
            Get
                Return pEnumItemsSorce
            End Get
            Set(ByVal value As [Enum])
                pEnumItemsSorce = value
                Me.ItemsSource = [Enum].GetValues(value.GetType)

            End Set
        End Property


        Public Function GetEnumItemSource(obj As DependencyObject) As [Enum]
            Return EnumItemsSorce
        End Function

        Public Sub SetEnumItemSource(obj As DependencyObject, value As [Enum])
            EnumItemsSorce = value
        End Sub

        ' Using a DependencyProperty as the backing store for Enum.  This enables animation, styling, binding, etc...
        Public Shared ReadOnly EnumItemSourceProperty As DependencyProperty = DependencyProperty.RegisterAttached("EnumItemSource", GetType([Enum]), GetType(EnumComboBox), New PropertyMetadata(Nothing, AddressOf OnEnumItemSourceChanged))

        Private Shared Sub OnEnumItemSourceChanged(sender As DependencyObject, e As DependencyPropertyChangedEventArgs)
            Dim control = TryCast(sender, ItemsControl)

            If control IsNot Nothing Then
                If e.NewValue IsNot Nothing Then
                    Dim _enum = [Enum].GetValues(TryCast(e.NewValue, Type))
                    control.ItemsSource = _enum
                End If
            End If
        End Sub

        Protected Overrides Sub OnItemContainerStyleSelectorChanged(oldItemContainerStyleSelector As System.Windows.Controls.StyleSelector, newItemContainerStyleSelector As System.Windows.Controls.StyleSelector)
            MyBase.OnItemContainerStyleSelectorChanged(oldItemContainerStyleSelector, newItemContainerStyleSelector)
        End Sub

        Protected Overrides Sub OnSelectionChanged(e As System.Windows.Controls.SelectionChangedEventArgs)
            MyBase.OnSelectionChanged(e)
        End Sub

        Private _enumType As Type

        Private Sub GetValues(ByVal E As [Enum])
            Dim result As New List(Of String)

            Dim _enumType As Type
            _enumType = E.GetType
            Dim enumValues = [Enum].GetValues(_enumType)

            Dim f = (From enumValue In enumValues Select New EnumerationMember() With { _
              .Value = enumValue, _
              .Description = enumValue.GetDescription(enumValue) _
            }).ToList

            _EnumList = f
        End Sub

        Private Function GetDescription(enumValue As Object) As String
            Dim descriptionAttribute = TryCast(_enumType.GetField(enumValue.ToString()).GetCustomAttributes(GetType(ComponentModel.DescriptionAttribute), False).FirstOrDefault(), ComponentModel.DescriptionAttribute)
            Return If(descriptionAttribute IsNot Nothing, descriptionAttribute.Description, enumValue.ToString())
        End Function

        Public Class EnumerationMember
            Public Property Description() As String
                Get
                    Return m_Description
                End Get
                Set(value As String)
                    m_Description = value
                End Set
            End Property
            Private m_Description As String
            Public Property Value() As Object
                Get
                    Return m_Value
                End Get
                Set(value As Object)
                    m_Value = value
                End Set
            End Property
            Private m_Value As Object
        End Class

    End Class


The Idea is to bind the Enum property "Something" dire3ctly to the combobox, mewaning when I change the combobox index the property in the Test class also changes.


Question is: How can I alter the code above to make this happen?
AnswerRe: Binding Enum to a ComboBox Pin
Mycroft Holmes28-May-13 14:22
professionalMycroft Holmes28-May-13 14:22 
GeneralRe: Binding Enum to a ComboBox Pin
Kenneth Haugland28-May-13 14:26
mvaKenneth Haugland28-May-13 14:26 
GeneralRe: Binding Enum to a ComboBox Pin
Mycroft Holmes28-May-13 14:33
professionalMycroft Holmes28-May-13 14:33 
GeneralRe: Binding Enum to a ComboBox Pin
Kenneth Haugland28-May-13 14:35
mvaKenneth Haugland28-May-13 14:35 
GeneralRe: Binding Enum to a ComboBox Pin
Mycroft Holmes28-May-13 14:50
professionalMycroft Holmes28-May-13 14:50 
GeneralRe: Binding Enum to a ComboBox Pin
Kenneth Haugland28-May-13 15:46
mvaKenneth Haugland28-May-13 15:46 
QuestionRe: Binding Enum to a ComboBox Pin
Kenneth Haugland29-May-13 7:53
mvaKenneth Haugland29-May-13 7:53 
AnswerRe: Binding Enum to a ComboBox Pin
Kenneth Haugland31-May-13 21:05
mvaKenneth Haugland31-May-13 21:05 
QuestionData binding problem Pin
columbos1492728-May-13 3:12
columbos1492728-May-13 3:12 
AnswerRe: Data binding problem Pin
Abhinav S28-May-13 3:58
Abhinav S28-May-13 3:58 
QuestionNeed suggestion on application architecture. Pin
Beniston120528-May-13 0:05
Beniston120528-May-13 0:05 
AnswerRe: Need suggestion on application architecture. Pin
Abhinav S28-May-13 3:57
Abhinav S28-May-13 3:57 
GeneralRe: Need suggestion on application architecture. Pin
Beniston120528-May-13 20:14
Beniston120528-May-13 20:14 
AnswerRe: Need suggestion on application architecture. Pin
SledgeHammer0128-May-13 6:56
SledgeHammer0128-May-13 6:56 
GeneralRe: Need suggestion on application architecture. Pin
Beniston120528-May-13 20:14
Beniston120528-May-13 20:14 
AnswerRe: Need suggestion on application architecture. Pin
Mycroft Holmes28-May-13 14:13
professionalMycroft Holmes28-May-13 14:13 
QuestionOne data context for multiple Controls Pin
columbos1492727-May-13 22:55
columbos1492727-May-13 22:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.