Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

ComboBox binding with Dictionary. RSS

6 replies

Last post Jul 16, 2009 11:18 PM by andresguti

(0)
  • kutabale

    kutabale

    0 Points

    20 Posts

    ComboBox binding with Dictionary.

    Nov 24, 2008 10:20 PM | LINK

    I have a combobox bound to Dictionary<string,string>   with key and value. Display memberpath is set to the value. But i want to store the selected value in the string property in the same dataObject. how can i accomplish this with out firing events.

  • preishuber

    preishuber

    Contributor

    3572 Points

    658 Posts

    Re: ComboBox binding with Dictionary.

    Nov 25, 2008 06:52 AM | LINK

    same DataObject? in meaning of Dictionary? where sould be a place to store selected?

    Combobox have selectedvalue

    -Hannes

    http://www.ppedv.de
  • Jonathan Shen – MSFT

    Jonathan She...

    All-Star

    50156 Points

    4951 Posts

    Microsoft

    Re: ComboBox binding with Dictionary.

    Nov 29, 2008 03:57 PM | LINK

     Hi Kutabale,

    As far as I know,  there is no direct way to achieve your goal.  Below is the alternative way for your situation.

          <ComboBox x:Name="myComboBox" Width="150" Height="30" SelectionChanged="myComboBox_SelectionChanged" DisplayMemberPath="Value" >
               
          </ComboBox>

     

            void Page_Loaded(object sender, RoutedEventArgs e)
    {
    Dictionary<int, string> myDic = new Dictionary<int, string>();
    myDic.Add(1, "itme1");
    myDic.Add(2, "itme2");
    myDic.Add(3, "itme3");
    myComboBox.ItemsSource = myDic;
    }

    private void myComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
    KeyValuePair<int, string> curItem =(KeyValuePair<int, string>) myComboBox.SelectedItem;
    MessageBox.Show(curItem.Key.ToString());
    }

      Best regards,

     Jonathan

     

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
  • Jonathan Shen – MSFT

    Jonathan She...

    All-Star

    50156 Points

    4951 Posts

    Microsoft

    Re: ComboBox binding with Dictionary.

    Dec 01, 2008 01:57 AM | LINK

    Hi all,

    Share another sample with our community members.  http://www.silverlightshow.net/items/Silverlight-ComboBox.aspx

    Best regards,

    Jonathan

    Please mark the replies as answers if they help or unmark if not.
    If you have any feedback about my replies, please contact msdnmg@microsoft.com.
    Microsoft One Code Framework
  • kutabale

    kutabale

    0 Points

    20 Posts

    Re: ComboBox binding with Dictionary.

    Dec 10, 2008 12:24 AM | LINK

    resolved my problem. by binding the combobox selected item and creating a converter for the selected item Mode=twoWay. My source for the combobox is actually static dictionary so i can have access in the converter. But you can i believe so pass the dictionary source as parameter in the SelecedItem.  evertime i Change the selected i tem will convert it for me.

    <ComboBox x:Name="name" Grid.Column="2" Grid.Row="1" Grid.ColumnSpan="2" DisplayMemberPath="Value" ItemsSource="{Binding Source , Source={StaticResource Source}}" SelectedItem="{Binding object.propertyName,Converter={StaticResource Converter},Mode=TwoWay}">

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

    ComboboxSource source = new ComboBoxSource();  // static source

    string dictonatyValue = string.Empty;

    source.TypeSource.TryGetValue((string)value, out Value);

    KeyValuePair<string, string> item = new KeyValuePair<string, string>((string)value,dictonaryValue );

    return item;

    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

    {

    var item = (KeyValuePair<string, string>)value;

    return item.Key;

    }

  • soshekar

    soshekar

    Member

    53 Points

    25 Posts

    Re: ComboBox binding with Dictionary.

    Dec 16, 2008 12:57 PM | LINK

    This is very nice approach, i feel very confortable using this piece of code.

    Thanks for this. :)

  • andresguti

    andresguti

    Member

    3 Points

    4 Posts

    Re: Re: ComboBox binding with Dictionary.

    Jul 16, 2009 11:18 PM | LINK

    hi

     i have problem whit this code.

    i try to implement this Convert in my project and i have this error.

    Error 6 The name 'dictonaryValue' does not exist in the current context C:\Documents and Settings\Andres G\Mis documentos\Visual Studio 2008\Projects\SITIO_W\SITIO_W\convertidores\comboxconvert.cs 24 97 SITIO_W

    I happens whit the  following statements

    -ComboboxSource

    -Value

    -dictonaryValue

    i could not help me understand a lot of SL3