How to bind a ComboBox to generic dictionary via ObjectDataProvider

前端 未结 3 549
有刺的猬
有刺的猬 2020-12-04 17:35

I want to fill a ComboBox with key/value data in code behind, I have this:

XAML:



        
相关标签:
3条回答
  • 2020-12-04 18:08

    There's an easier way.

    Convert the enumeration to a Generic.Dictionary object. For example let say you wanted a combo box with the weekday ( just convert the VB to C#)

    Dim colWeekdays As New Generic.Dictionary(Of FirstDayOfWeek, String)
        For intWeekday As FirstDayOfWeek = vbSunday To vbSaturday
           colWeekdays.Add(intWeekday, WeekdayName(intWeekday))
        Next
    
    RadComboBox_Weekdays.ItemsSource = colWeekdays
    

    In your XAML you only need to set the following to bind to an object:

    SelectedValue="{Binding Path= StartDayNumberOfWeeek}"  SelectedValuePath="Key" 
    DisplayMemberPath="Value" />
    

    The code above can easily be generalized using reflection to handle any enumerations.

    hope this helps

    0 讨论(0)
  • 2020-12-04 18:21

    The way DevExpress 17.1.7 handles this is setting those properties: DisplayMember and ValueMember, in case of a dictionary it would be something like this:

    DisplayMember="Value" 
    ValueMember="Key"
    
    0 讨论(0)
  • 2020-12-04 18:22

    To your ComboBox add

    SelectedValuePath="Key" DisplayMemberPath="Value"
    
    0 讨论(0)
提交回复
热议问题