Binding when a property is not observed

Is there a better way to bind a property of a list of objects when it is not observed? For example, if we have:

public class Band
{
    public string Name { get; set; }
    public List<Musician> Members { get; private set; }
    // other properties here
}

If my view model has a property Bandand I want my user interface to bind to a property Members, how are the properties Membersspecified in the user interface updated ? Members are not observed.

The current thought is to create a DTO, which is an exact copy of the class Band, but create Membersa ObservableCollection. Then the user interface will be tied to this DTO. Before the view model closes, it returns the DTO to the appropriate class Band. This is like overhead. Is there a better way?

Change is another option

I just had a discussion, personally, with someone about this. The idea is to add a property ObservableCollectionto the view model. Then, in the constructor of the view model, use the object to fill ObservableCollection. After user editing is completed, use updated ObservableCollectionto reset Listfor the object.

+4
source share
2 answers

As you mentioned, you can use ObservableCollection. You can also use BindingList.

, , , Band INotifyPropertyChanged, PropertyChanged Members name. , .

, , , , PropertyChanged, ObservableCollection<T> BindingList<T>.

, , , , , Refresh. - - , , .

MVVM, View BandViewModel ObservableCollection MemberViewModel s. , ViewModel , - .

+2

. : ObservableCollection.

ObservableCollection, , , , . , ( , ). /// ... , IEnumerable!

, , , (IEnumerable Observable). ( ), ( INotifyPropertyChanged).

, !

0

Source: https://habr.com/ru/post/1527015/


All Articles