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

Asked

Viewed 277 times

2

I would like a brief explanation of how to fill a MVVM standard combobox. The three parts, Model, View and Viewmodel filled with the table of the Database.

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 don’t know how to implement Viewmodel so that it fills the Combobox with the Access table data that are Cd_raca and Nm_raca

  • Try to implement or if you already have some pole code so we can help.

  • Enter the code

1 answer

1

Hello!

I use the Viewmodel to assemble the list of items I want to popular the combobox.

For example:

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

The variables 'List items' and 'Itemsselected' are properties of Viewmodel of any type. 'Itemselected' may also be some attribute of an object.

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

Hugs!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.