Click here to Skip to main content
15,902,198 members
Home / Discussions / WPF
   

WPF

 
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 
I found a solution, so now I can bind an Enum property to a custom combobox without any clutter in Code behind or XAML:
VB
Imports System.ComponentModel

Namespace WPF
    Public Class COmboBoxAttached
        Inherits ComboBox

        Private _EnumList As New List(Of EnumerationMember)

        Private DontRebind As Boolean = False

        Private pEnumItemsSorce As [Enum]
        Public Property EnumItemsSorce() As [Enum]
            Get
                Return pEnumItemsSorce
            End Get
            Set(value As [Enum])
                If Not DontRebind Then
                    GetValues(value)
                    Me.ItemsSource = _EnumList
                    Me.DisplayMemberPath = "Description"
                    Me.SelectedValue = "Value"
                End If

                DontRebind = False
                pEnumItemsSorce = value
            End Set
        End Property

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

            If Me.SelectedValue IsNot Nothing Then
                DontRebind = True
                EnumItemsSorce = DirectCast(Me.SelectedValue, EnumerationMember).Value
            End If

            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 = DirectCast(enumValue, [Enum]).GetDescription _
            }).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
End Namespace

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 
AnswerRe: One data context for multiple Controls Pin
AlphaDeltaTheta28-May-13 1:13
AlphaDeltaTheta28-May-13 1:13 
GeneralRe: One data context for multiple Controls Pin
columbos1492728-May-13 3:07
columbos1492728-May-13 3:07 
QuestionRichtextbox text overlaps Pin
Antony_Christopher27-May-13 20:34
Antony_Christopher27-May-13 20:34 
QuestionSilverlight do we have a road map for the future Pin
Mycroft Holmes26-May-13 14:43
professionalMycroft Holmes26-May-13 14:43 
AnswerRe: Silverlight do we have a road map for the future Pin
SledgeHammer0128-May-13 6:54
SledgeHammer0128-May-13 6:54 
GeneralRe: Silverlight do we have a road map for the future Pin
Mycroft Holmes28-May-13 14:07
professionalMycroft Holmes28-May-13 14:07 
GeneralRe: Silverlight do we have a road map for the future Pin
_Maxxx_28-May-13 18:13
professional_Maxxx_28-May-13 18:13 
GeneralRe: Silverlight do we have a road map for the future Pin
Mycroft Holmes28-May-13 19:49
professionalMycroft Holmes28-May-13 19:49 

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.