Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Binding an Enum Property to a ComboBox

$
0
0

I have the folloing code:

Imports System.ComponentModel

Class MainWindow
    Dim ClassWithEnumProperty As New EnumPropertyTest
    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Some.EnumItemsSorce = ClassWithEnumProperty.SelectedEnumValue
    End Sub

    Private Sub btnName_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnName.Click
        MessageBox.Show(ClassWithEnumProperty.SelectedEnumValue.GetDescription())
    End Sub

    Private Sub cmbSomething_SelectionChanged(sender As System.Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles Some.SelectionChanged
        ClassWithEnumProperty.SelectedEnumValue = Some.EnumItemsSorce
    End Sub
End Class

Class EnumPropertyTest
    Public Enum EventDescription As Integer
        <Description("Something happended")> Something<Description("Nothing happended")> Nothing_
    End Enum

    Private pSelectedEnumValue As EventDescription
    Public Property SelectedEnumValue() As EventDescription
        Get
            Return pSelectedEnumValue
        End Get
        Set(ByVal value As EventDescription)
            pSelectedEnumValue = value
        End Set
    End Property
End Class

That has this XAML:

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:cmb="clr-namespace:WpfComboBoxEnumBinding.WPF"     
    Title="MainWindow" Height="350" Width="525"><Window.Resources></Window.Resources><StackPanel><cmb:COmboBoxAttached x:Name="Some" SelectionChanged="cmbSomething_SelectionChanged" ></cmb:COmboBoxAttached><Button Name="btnName">OK</Button></StackPanel></Window>

And I designed the folowing custom control:

Imports System.ComponentModel
Imports System.Reflection

Namespace WPF
    Public Class COmboBoxAttached
        Inherits ComboBox

        Private _EnumList As New List(Of EnumerationMember)
        Private _EnumString As New List(Of String)

        Private pEnumItemsSorce As [Enum]
        Public Property EnumItemsSorce() As [Enum]
            Get
                Return pEnumItemsSorce
            End Get
            Set(value As [Enum])
                GetValues(value)
                Me.ItemsSource = _EnumString
                pEnumItemsSorce = value
            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.Register("EnumItemSource", GetType([Enum]), GetType(COmboBoxAttached), New PropertyMetadata(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 OnSelectionChanged(e As System.Windows.Controls.SelectionChangedEventArgs)
            Dim f As Object = e.AddedItems.Item(0).ToString
            Dim g As Integer = _EnumString.IndexOf(f)

            EnumItemsSorce = DirectCast(_EnumList(g).Value, [Enum])
            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

            _EnumString.Clear()

            For Each m As EnumerationMember In _EnumList
                _EnumString.Add(m.Description)
            Next

        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

What do I have to do to bind the SelectedEnumValue to the EnumITemsSource toghether? I can get the EnumItemsSorce to update but I cant get the value to update in the control, any ideas?




Kenneth


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>