Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. How to design using MVVM for a GUI interacting with Hardware

How to design using MVVM for a GUI interacting with Hardware

Scheduled Pinned Locked Moved WPF
wpfcsharpwinformsdesignhardware
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Praveen Raghuvanshi
    wrote on last edited by
    #1

    Hi, I am new to MVVM and trying to implement it in my application. I have a develop a GUI which will interact with the Hardware through some protocol. This is first experience under this space. Just to keep things simple, consider I have a hardware which contains Voltage and Resistance parameter at different sections of the hardware. View ---- So, I have total 5 sections. Each of them contains 15 different Voltages and Resistance. In my WPF GUI, I am representing these 5 sections in a tab control. I have created a user control(PowerControl) that contains 2 sliders for Voltage and Resistance. Voltage varies from 0-120v and Resistance varies from 1-10 ohm. Each of the sections in the tab control contains 15 user controls. Model ----- I am planning to create a class Power which has 2 properties for voltage and Resistance. ViewModel --------- I am planning to create a class 'PowerViewModel' to represent the Power model. It will have two properties which will have INotifyProperty changed event. The change from the slider has to be conveyed to the hardware and it is done through a set of uniquely identified parameter. Such as if a slider on the PowerControl of section 2(tab2) is moved, it 'll be represented as "PC2, section2, value of slider". So, its a combination of these three things and I used to send it the hardware which displays it in the hardware display. Here are my questions. 1- Where should I write the unique ID's? Should it be in the Model or ViewModel? 2- How should we bind the slider as per their ID's? 3- How does my Model know which slider has been moved? 4- How can we apply MVVM here? Thanks in advance and please feel free to ask for any clarification on this.

    Praveen Raghuvanshi Software Developer

    M L 2 Replies Last reply
    0
    • P Praveen Raghuvanshi

      Hi, I am new to MVVM and trying to implement it in my application. I have a develop a GUI which will interact with the Hardware through some protocol. This is first experience under this space. Just to keep things simple, consider I have a hardware which contains Voltage and Resistance parameter at different sections of the hardware. View ---- So, I have total 5 sections. Each of them contains 15 different Voltages and Resistance. In my WPF GUI, I am representing these 5 sections in a tab control. I have created a user control(PowerControl) that contains 2 sliders for Voltage and Resistance. Voltage varies from 0-120v and Resistance varies from 1-10 ohm. Each of the sections in the tab control contains 15 user controls. Model ----- I am planning to create a class Power which has 2 properties for voltage and Resistance. ViewModel --------- I am planning to create a class 'PowerViewModel' to represent the Power model. It will have two properties which will have INotifyProperty changed event. The change from the slider has to be conveyed to the hardware and it is done through a set of uniquely identified parameter. Such as if a slider on the PowerControl of section 2(tab2) is moved, it 'll be represented as "PC2, section2, value of slider". So, its a combination of these three things and I used to send it the hardware which displays it in the hardware display. Here are my questions. 1- Where should I write the unique ID's? Should it be in the Model or ViewModel? 2- How should we bind the slider as per their ID's? 3- How does my Model know which slider has been moved? 4- How can we apply MVVM here? Thanks in advance and please feel free to ask for any clarification on this.

      Praveen Raghuvanshi Software Developer

      M Offline
      M Offline
      Mark Salsbery
      wrote on last edited by
      #2

      I'll take a stab here... If the IDs don't need to be in the model then they should be used in the viewmodel. But, if you have separate objects in your view model - each associated with individual PowerViewModel objects, then you can bind those to each user control. Then you don't need IDs and the model knows what slider is moved.

      Mark Salsbery Microsoft MVP - Visual C++ :java:

      modified on Tuesday, May 24, 2011 4:53 PM

      P 1 Reply Last reply
      0
      • P Praveen Raghuvanshi

        Hi, I am new to MVVM and trying to implement it in my application. I have a develop a GUI which will interact with the Hardware through some protocol. This is first experience under this space. Just to keep things simple, consider I have a hardware which contains Voltage and Resistance parameter at different sections of the hardware. View ---- So, I have total 5 sections. Each of them contains 15 different Voltages and Resistance. In my WPF GUI, I am representing these 5 sections in a tab control. I have created a user control(PowerControl) that contains 2 sliders for Voltage and Resistance. Voltage varies from 0-120v and Resistance varies from 1-10 ohm. Each of the sections in the tab control contains 15 user controls. Model ----- I am planning to create a class Power which has 2 properties for voltage and Resistance. ViewModel --------- I am planning to create a class 'PowerViewModel' to represent the Power model. It will have two properties which will have INotifyProperty changed event. The change from the slider has to be conveyed to the hardware and it is done through a set of uniquely identified parameter. Such as if a slider on the PowerControl of section 2(tab2) is moved, it 'll be represented as "PC2, section2, value of slider". So, its a combination of these three things and I used to send it the hardware which displays it in the hardware display. Here are my questions. 1- Where should I write the unique ID's? Should it be in the Model or ViewModel? 2- How should we bind the slider as per their ID's? 3- How does my Model know which slider has been moved? 4- How can we apply MVVM here? Thanks in advance and please feel free to ask for any clarification on this.

        Praveen Raghuvanshi Software Developer

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        For MVVM you're kinda doing it the wrong way around - you should create your VM first and then design a view. Anyhow, I know lots of people work better visually, so: You have five sections each with a collection of 'Voltages and resistances' So you want a SectionViewModel that contains a collection of VoltageAndResistanceViewModels - each of which has a Voltage property and a resistance property -which utilise INotifyPropertyChanged Then you want another ViewModel that contains a collection of the SectionViewModel I'd say that when you instantiate each of your ViewModels you'd need to tell them which unique Id for the hardware they are representing. This information (I would htink) would be contained in the model as a series fo collectins - so you would construct your collection of ViewModels based upon these collections, hence the Model has the unique Ids, which are referenced by the viewModels Each slider would be bound in Xaml to the appropriate property of the particular VM to which the View is bound When the property of the Vm is changed, if necessary that's the time to adjust the property on the model - the model can then do whatever it does in order to update the machine's value. Hope that helps at least a little!

        MVVM# - See how I did MVVM my way ___________________________________________ .\\axxx (That's an 'M')

        P 1 Reply Last reply
        0
        • M Mark Salsbery

          I'll take a stab here... If the IDs don't need to be in the model then they should be used in the viewmodel. But, if you have separate objects in your view model - each associated with individual PowerViewModel objects, then you can bind those to each user control. Then you don't need IDs and the model knows what slider is moved.

          Mark Salsbery Microsoft MVP - Visual C++ :java:

          modified on Tuesday, May 24, 2011 4:53 PM

          P Offline
          P Offline
          Praveen Raghuvanshi
          wrote on last edited by
          #4

          Appreciate the quick response. I'll try it.

          Praveen Raghuvanshi Software Developer

          1 Reply Last reply
          0
          • L Lost User

            For MVVM you're kinda doing it the wrong way around - you should create your VM first and then design a view. Anyhow, I know lots of people work better visually, so: You have five sections each with a collection of 'Voltages and resistances' So you want a SectionViewModel that contains a collection of VoltageAndResistanceViewModels - each of which has a Voltage property and a resistance property -which utilise INotifyPropertyChanged Then you want another ViewModel that contains a collection of the SectionViewModel I'd say that when you instantiate each of your ViewModels you'd need to tell them which unique Id for the hardware they are representing. This information (I would htink) would be contained in the model as a series fo collectins - so you would construct your collection of ViewModels based upon these collections, hence the Model has the unique Ids, which are referenced by the viewModels Each slider would be bound in Xaml to the appropriate property of the particular VM to which the View is bound When the property of the Vm is changed, if necessary that's the time to adjust the property on the model - the model can then do whatever it does in order to update the machine's value. Hope that helps at least a little!

            MVVM# - See how I did MVVM my way ___________________________________________ .\\axxx (That's an 'M')

            P Offline
            P Offline
            Praveen Raghuvanshi
            wrote on last edited by
            #5

            Appreciate the quick response. Very well explained. I'll try it

            Praveen Raghuvanshi Software Developer

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups