ComboBox in WPF – How to populate using MVVM

Posted on

Question :

I am having doubts on how popular a ComboBox in WPF using the MVVM standard. Here in Stackoverflow there is a similar question to mine, but it was not answered ( # ).

I searched and saw that people mixed the Persistence layer a bit with View (code-behind). I believe they are not obeying the MVVM.

I would like to know how best to list items in a ComboBox that are in the database using the MVVM standard.

ps: If I am wrong about the MVVM pattern, please explain why.


Answer :


I use the ViewModel to assemble the list of items I want to populate the combobox.

For example:

<ComboBox ItemsSource="{Binding ListaItens}" SelectedItem="{Binding ItemSelecionado}" />

Variables ‘ List ‘ and ‘ ItemSelected ‘ are ViewModel properties of any type. The ‘Selected Item’ may also be some attribute of an object.



Leave a Reply

Your email address will not be published. Required fields are marked *