So I have a primary MainViewModel that collects and distributes a fair amount of real-time data (changes every .1 seconds). Currently all of my Controls are referencing my MainViewModel.
I’m using a UserControl Control to replicate many objects of the same type of Digital Gauge.
My question is, is there a best practice for “pushing” or “referencing” data from my MainViewModel to a ControlViewModel? The purpose I have in mind is to keep the DependencyProperties I need tucked away cleanly in a ControlViewModel and keep the MainViewModel as clean and tidy as possible.
Is MVVMLight Messaging an option?
Is an event appropriate?
Can I setup bindings from the ControlViewModel to the MainViewModel so when the MainViewModel data is updated, it gets reflected in the ControlViewModel?
If I understand the question properly, I think you may just want to create a property of that exposes a collection of
ControlViewModels in yourMainViewModel:You can set this with all of the necessary
ControlViewModelobjects in the constructor. If you might add or remove gauges, changeIEnumerabletoObservableCollection.Then for your gauges, you’d use an
ItemsControland setItemsSource="{Binding Controls}". This will automatically make theDataContextfor each item one of yourControlViewModels.To render the items with your gauge control, set the
ItemTemplateon theItemsControl: