0
votes

I have a User Control for searching container numbers. If the user enters a container number that's new to the system then I want to tell the VM "I have a new record to add".

The MVVM method avoids using Events to communicate with the VM as they create code-behind. Should I create a Dependency Property to trigger the VM but I don't think I've seen other controls with a "NewRecord" property?

Any thoughts?

2

2 Answers

0
votes

A good way is to use the Mediator pattern - most MVVM libraries will have some sort of pub/sub mechanism, eg "Messenger" in MvvmLight or EventAggregator in Prism.

Basically, your 'container search VM' publishes a "NewRecord" message, probably using the record instance as a payload, and the interested part(s) of the app subscribe to that message and do what they need to do with the object as it comes in, like showing it in a list...

Hope this helps,

0
votes

ObservableCollection

Take a look at the ObservableCollection class. Specifically, when you add a new item to this collection it will fire a INotifyCollectionChanged event with NotifyCollectionChangedAction.Add and NewItems set to the added item.

This class is what's commonly used in MVVM for data binding to a changing collection of items. You should be able to lightly couple this to your UI via data binding. It should also help you deal with 'a 2nd item has been added before I completed my new record handling' scenarios as you work out your apps' behavior.