Binding a Dictionary to a ComboBox

View code (XAML):

<ComboBox ItemsSource="{Binding Items}"
  DisplayMemberPath="Value"
  SelectedValuePath="Key"
  SelectedValue="{Binding SelectedItemCode}"/>

ViewModel code:

// Key = ItemCode, Value = Name
public IDictionary<string, string> Items{get; private set;}  

public string SelectedItemCode
{
 get
 {
  return Settings.Default.SelectedItemCode;
 }
 set
 {
  if (Settings.Default.SelectedItemCode != value)
  {
   Settings.Default.SelectedItemCode = value;
   OnPropertyChanged("SelectedItemCode");
  }
 }
}

A settings file is used to store the user selection. Of cause you have to save the settings e.g. if your view is closed.

Share and Enjoy:
  • Add to favorites
  • RSS
  • Twitter
  • DotNetKicks
  • DZone
  • email
Dieser Beitrag wurde unter Windows Presentation Foundation abgelegt und mit , , , , verschlagwortet. Setze ein Lesezeichen auf den Permalink.

Hinterlasse eine Antwort

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind markiert *

*

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>