Binding Linked Combobox Update

Asked

Viewed 108 times

1

I’m working with the standard MVVM, so far so good, I have a class PessoaViewlModel.

In it I own a property IEnumerable<Municipio> Municipios, in which he presents to me all the municipalities, in accordance with UF(Unidade federativa Selecionada).

When I start WPF of person registration, it loads the municipalities, but after initialized step the command so that in the list are only the municipalities that correspond to UF. On my own property Municipios of PessoaViewModel is correct, but the items in the combobox does not follow faithfully the information in Municipios to which the combobox is linked with Binding.

2 answers

2

Use ObservableCollection<Municipio> in place of IEnumerable<Municipio>. Whenever an element enters the exits or a list is generated a notification, making the screen modify their elements.

Do not create a new object, as you will lose the connection unless your class implements the interface INotifyPropertyChanged and you have the property

ObservableCollection<Municipio> _municipios;
public ObservableCollection<Municipio> Municipios
{
    get { return _municipios; }
    set 
    {
        if(_municipios != value) 
        {
            _municipios = value;
            PropertyChanged(this, new PropertyChangedEventArgs("Municipios"));
        }
    }
}

This way you can still use the IEnumerable<Municipio> but it won’t work if you add some new element to the list.

0


I managed to solve the problem, actually I was working with frmaework Mvvmmicro, I switched to Mvvm light Toolkit and it worked normally. Mumbling that all the tips they gave me tried and didn’t work.

Browser other questions tagged

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