Simple WPF + MVVM Binding

I have a class with a name MyWindowderived from Window. I use the MVVM pattern, so in the encoding I have the following field:

public MyViewModel ViewModel = new MyViewModel();

ViewModelcontains a collection Person, and all I would like to do is associate ComboBoxwith this collection, show Person.Nameas a title for each Person.
I would also like to have another field in ViewModelwhich will be bound to the data for the selected item.

Please help me.

+5
source share
2 answers

Well, firstly, you need to set the datacontext of your window in viewmdodel in the constructor, if you haven't done it yet:

this.DataContext = MyModelView;

ComboBox :

<ComboBox ItemsSource={Binding Persons} SelectedItem={Binding CurrentPerson,Mode=TwoWay} DisplayMemberPath="Name"/>

, , - , .

+5
  • modelView MyWindow.DataContext. .
  • combobox xaml :

<ComboBox ItemsSource="{Binding PersonCollection}" DisplayMemberPath="Name" SelectedValue="{Binding SelectedPerson}" > </ComboBox>

, modelView PersonCollection, Person, Person SelectedPerson modelView Person.

+2

All Articles