I have a Dictionary<String,Person>
, where Person is a class, defined below
String role;
public class Person
{
public string firstname{ get; set; }
public string lastname{ get; set; }
public string city{ get; set; }
}
My question is how to bind the Dictionary key: String with a ComboBox, meanwhile. have the Dictionary value: Person connected with thr开发者_运维问答ee textboxes. That is, once a key is selected in the ComboBox, the corresponding value, firstname, lastname, city are shown in the three textboxes respectively?
Thanks in advance!
Note: Not copy-pastable code... exists solely to get the point across.
View
I see some controls with the databinding as shown below in the Window/View...
<Combobox ItemsSource="{Binding Path=AlphabeticalIds}" Text="{Binding Path=SelectedId}"/>
<TextBox Text="{Binding Path=SelectedPerson.FirstName}"/>
<TextBox Text="{Binding Path=SelectedPerson.LastName}"/>
<TextBox Text="{Binding Path=SelectedPerson.City}"/>
ViewModel
An instance of this class is set as the datacontext of the view. It has the following members. (They're exposed as .NET Properties ; NotifyPropertyChange calls identify the properties that require change notification)
_mapIdToPerson = Dict...
_alphabeticalIds = _map...Keys.Sort();
_selectedId;
set
{
_selectedId = value;
_selectedPerson = _mapIdToPerson[_selectedId];
NotifyPropertyChange("SelectedId");
NotifyPropertyChange("SelectedPerson");
}
_selectedPerson;
精彩评论