How to bind a ComboBox to generic dictionary via ObjectDataProvider

C#WpfXamlData BindingCombobox

C# Problem Overview


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

XAML:

<Window x:Class="TestCombo234.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestCombo234"
    Title="Window1" Height="300" Width="300">
    <Window.Resources>
        <ObjectDataProvider x:Key="Choices" ObjectType="{x:Type local:CollectionData}" MethodName="GetChoices"/>
    </Window.Resources>
    <StackPanel HorizontalAlignment="Left">
        <ComboBox ItemsSource="{Binding Source={StaticResource Choices}}"/>
    </StackPanel>
</Window>

Code Behind:

using System.Windows;
using System.Collections.Generic;

namespace TestCombo234
{
    public partial class Window1 : Window
    {
        public Window1()
        {
            InitializeComponent();
        }
    }

    public static class CollectionData
    {
        public static Dictionary<int, string> GetChoices()
        {
            Dictionary<int, string> choices = new Dictionary<int, string>();
            choices.Add(1, "monthly");
            choices.Add(2, "quarterly");
            choices.Add(3, "biannually");
            choices.Add(4, "yearly");
            return choices;
        }
    }
}

What do I have to change so that the key is the int and the value is the string?

C# Solutions


Solution 1 - C#

To your ComboBox add

SelectedValuePath="Key" DisplayMemberPath="Value"

Solution 2 - C#

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

Solution 3 - C#

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"

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionEdward TanguayView Question on Stackoverflow
Solution 1 - C#Bryan AndersonView Answer on Stackoverflow
Solution 2 - C#JimView Answer on Stackoverflow
Solution 3 - C#juagicreView Answer on Stackoverflow