jeudi 6 août 2020

wpf CheckBox command parameter

In my View I have 8 checkboxes all bound to a different property in the Model, lets say bool port1 ... port8. The property changes when the concerned checkbox is clicked. When the user checks or unchecks one of these checkboxes, I also execute a binded command 'SetPortCommand'. The command executes a function like SetPort(uint numPort, bool set) My view looks like this:

<CheckBox x:Name="cbPort1" Content="port1" Command="{Binding SetPortCommand}">
    <CheckBox.IsChecked>
        <Binding Path="ADQ_63_Model.Port1"/>
    </CheckBox.IsChecked>
</CheckBox>

Behind the whole thing is quite slow hardware, so I would like to avoid calling the function SetPort() for each port. I could use 8 commands like SetPortCommand1 to SetPortCommand8, but that causes lots of duplicate code. I had the following ideas, but I do not know how to implement it, neither which one wouldf be conform to MVVM.

  1. idea would be to somehow pass a constant to the command, telling it which of the 8 ports it should check or uncheck. Is there a easy way to do this?
  2. idea would be to somehow use the OnPropertyChanged()-Event to call the function in here.

Any help appreciated :-)




Aucun commentaire:

Enregistrer un commentaire