MVVM mode: ComboBox Data Binding of WPF, using Dictionary as the data source, mvvmwpf
ViewModel
// Attribute definition Dictionary <int, string> _ selGroupList; // <summary> // group drop-down list /// </summary> public Dictionary <int, string> selGroupList {get {return _ selGroupList;} set {_ selGroupList = value; NotifyOfPropertyChange ("selGroupList");} private int _ Group; /// <summary> /// current Group /// </summary> public int Group {get {return _ Group;} set {_ Group = value; required yofpropertychange (() => Group );}}
// Initialize data // interface data public ModuleInfoViewModel (sys_Right_Module groupInfo, OperType type) {GetGroupList ();
Group = groupInfo. GroupID;
}
/// <Summary>
/// Initialize the group drop-down data
/// </Summary>
Public void GetGroupList ()
{
Dictionary <int, string> dic = new Dictionary <int, string> ();
Dic. Add (-1, "= select = ");
List <sys_Right_Group> groupList = DataBaseService. DataBaseServer <sys_Right_Group>. GetModelList ("IsActive = 1 ");
If (groupList! = Null)
{
GroupList. ForEach (x =>
{
Dic. Add (x. GroupID, x. GroupName );});
}
SelGroupList = dic;
Group =-1; // The first item is selected by default.
}
View Interface binding:
The ItemsSource data source is dictionary data.
DisplayMemberPath = "Value" is the Value for displaying Dictionary data.
SelectedValuePath = "Key" the dictionary data Key corresponds to the SelectedValue type
<ComboBox Grid.Row="8" Grid.Column="1" ItemsSource="{Binding selGroupList}" SelectedIndex="0" SelectedValuePath="Key" DisplayMemberPath="Value" SelectedValue="{Binding Group,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" HorizontalAlignment="Left" Width="252" Height="25" IsEditable="True" Margin="5,3"></ComboBox>
Interface effect: