0

If there are multiple instances of a user control, how can I use data binding to update a property of the same name, for each of the control?

for example, the following is a part of my main windows design:

myVoltCtrl is my control to display for example, voltage. and I have two of the controls for displaying samples from two sensors. I have a viewmodel exposing the voltage: ProgressPv.

My question is how does my ViewModel can distinguish which sensor it is updating? Or do I have to make two separate instances of viewmodels for each sensor control?

I did quite a lot of search, but still can not figure out how this can be done.

Thanks in advance.

            <Grid Grid.Row="0">
                <local:myVoltCtrl x:Name="Sensor1" MinHeight="5" MinWidth="5"  Volt="{Binding ProgessPv}">
                </local:myVoltCtrl>
            </Grid>
            <Grid Grid.Row="1">
                <local:myVoltCtrl x:Name="Sensor2" MinHeight="5" MinWidth="5"  Volt="{Binding ProgessPv}" >
                </local:myVoltCtrl>
            </Grid>
Chengting
  • 345
  • 2
  • 4
  • 11
  • 1
    Your ViewModel shouldn't care which use control it's updating, as it doesn't (and shouldn't know) of any user controls. If you have 2 different values then you'll need 2 different properties for your sensors to bind on to, something like `ProgressPv1` and `ProgressPv2` and bind on to them. The ViewModel then updates one or the other and raises a `PropertyChanged` event notifying to View there was a change – MindSwipe May 27 '20 at 08:36
  • 1
    A data binding only sends the value to the property. So the view model has no information who updates a property. This is a general fact. A class never knows who accesses a property or who invokes a method. Member access is anonymous. If this matters you can use dedicated properties for each sensor. Or use commands to update the value. You can pass e.g. a meta info object to the view model using the CommandParameter. But your view model shouldn't know about view. Why would the view model need to know which view updated a property? The view model just cares about the data. – BionicCode May 27 '20 at 08:39
  • 1
    Just bind each `Volt` property to **different** properties in your view model. e.g. `PortGeneratorVolts` and `StarboardGeneratorVolts`. You only need a single VM –  May 27 '20 at 08:46
  • Oh, I see! I have been sticking to the non-mvvm idea of relating data to view (i.e. make some data belong to specific view), while view truly should be data-source agnostic. – Chengting May 27 '20 at 11:18
  • By the way, though separate properties could do the work, is there any way to avoid such repetition code of property definitions? because this is usually done by an array or list : e.g. ctrlSensors[i].Volt= sampledVoltData[i]. Do I need to make a list of controls and a corresponding view list? – Chengting May 27 '20 at 11:23
  • maybe this is the solution to my question: https://stackoverflow.com/questions/52921418/how-can-i-use-an-array-in-a-viewmodel/52922872 – Chengting Jun 01 '20 at 05:50

0 Answers0