I would like a brief explanation of how to fill a combobox in MVVM pattern

2

I would like a brief explanation of how to populate a combobox in MVVM pattern. The three parts, Model, View and ViewModel populated with the Database table.

Model:

class Racas : INotifyPropertyChanged
{
    private int _cd_Raca;
    private string _nm_Raca;

    public int Cd_Raca
    {
        get { return _cd_Raca ; }
        set
        {
            _cd_Raca = value;
            NotifyOfPropertyChanged("Cd_Raca");
        }
    }
    public string Nm_Raca
    {
        get { return _nm_Raca; }
        set
        {
            _nm_Raca = value;
            NotifyOfPropertyChanged("Nm_Raca");
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    private void NotifyOfPropertyChanged(string propertyName)
    {
        if(PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

View (XAML - WPF):

<ComboBox x:Name="dsCmbRaca" HorizontalAlignment="Left"
 Margin="438,4,0,0" VerticalAlignment="Top" Width="94" Height="19"/>

I do not know how to implement the ViewModel so that it populates the ComboBox with the data of the Access table that are Cd_Raca and Nm_Raca

    
asked by anonymous 19.05.2015 / 05:17

1 answer

1

Hello

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}" />

The variables 'ListItems' and 'SelectedItem' are ViewModel properties of any type. The 'SelectedItem' can also be some attribute of an object.

So in the ViewModel I load this list with the items in the database, in my case I use Nhibernate as ORM to help.

Hugs!

    
08.10.2015 / 21:54