MMD MNC
MMD MNC

Reputation: 159

How to bind a ComboBox to generic dictionary with xaml code

I'm using the following code :

private Dictionary<string, string> GetNumber { get; set; }
public ReportsLetterTra()
{
    GetNumber = new Dictionary<string, string>
                            {
                                {"1", "First"},
                                {"2", "Second"}
                            };

    InitializeComponent();
}

xaml code :

 <ComboBox 
      DisplayMemberPath="value" 
      SelectedValuePath="key" 
      ItemsSource="{Binding ElementName=reportslettra,Path=GetNumber}"
      SelectedIndex="0" Name="cmbFromNumber" />

Why is not bind GetNumber to cmbFromNumber?!

Update :

my complete code in behind file :

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

namespace WpfApplication20
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        public Dictionary<string, string> GetNumber { get; set; }

        public Window1()
        {

            GetNumber = new Dictionary<string, string>
                                    {
                                        {"1", "First"},
                                        {"2", "Second"}
                                    };
            InitializeComponent();

        }
    }
}

my complete xaml code:

<Window x:Class="WpfApplication20.Window1" Name="eportslettra"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="300">
    <Grid Height="26">
        <ComboBox DisplayMemberPath="Value" SelectedValuePath="Key" 
               ItemsSource="{Binding ElementName=reportslettra,Path=GetNumber}" 
               SelectedIndex="0"  Name="cmbFromNumber"    />
    </Grid>
</Window>

Where is my wrong? Why is not bind GetNumber to cmbFromNumber?!

Upvotes: 0

Views: 5435

Answers (1)

devdigital
devdigital

Reputation: 34349

Your property is marked as private, make it public. Also, correct the casing on your ComboBox to Value and Key. Thirdly, your binding expression looks invalid, double check this. Finally, it looks like your properties are part of the view's code behind file. You might consider the MVVM design pattern.

Update

Your Window has the Name of 'eportslettra', but your binding expression uses the ElementName 'reportslettra'. Correct one of them.

Upvotes: 1

Related Questions