I'm experimenting with MVVM and I can't quite wrap my mind around it.
I have a View (windows) that has several repeated controls.
Let's say I have 4 textbox - button pairs. Their behavior should be the same, after pressing a button paired textbox says "Hello World!"
I have tried several options I could think of:
- One ViewModel, 4 string properties binded to textboxes, 1 command
- When I bind each button to the same command I can't tell which property needs to be set.
- Passing enum to CommandParameter feels awkward.
- One ViewModel and UserControl that hosts a textbox and a button repeated 4 times.
- Now I need to expose all the properties like Command, CommandParameter, Text etc.. Seems like a lot of work.
- Still can't say which property needs to be updated after click.
- Each UserControl has a ViewModel
- This solves button clicking and setting property, but now I have no clue how to extract texts from nested ViewModels to the window ViewModel.
Is there any other way? I suspect DataTemplates could be of use, but I'm not sure how.