wpf - Recommend best approach in this situation -


i'm experimenting mvvm , can't quite wrap mind around it.

i have view (windows) has several repeated controls.

let's have 4 textbox - button pairs. behavior should same, after pressing button paired textbox says "hello world!"

i have tried several options think of:

  1. one viewmodel, 4 string properties binded textboxes, 1 command
    • when bind each button same command can't tell property needs set.
    • passing enum commandparameter feels awkward.
  2. one viewmodel , usercontrol hosts textbox , button repeated 4 times.
    • now need expose properties command, commandparameter, text etc.. seems lot of work.
    • still can't property needs updated after click.
  3. each usercontrol has viewmodel
    • this solves button clicking , setting property, have no clue how extract texts nested viewmodels window viewmodel.

is there other way? suspect datatemplates of use, i'm not sure how.

what describe such abstract , contrived idea doesn't warrant mvvm. you're talking textboxes , buttons, 'view', , not mvvm way of thinking. you'd start model.

there no 'model' per-se here though; specification literally set value of textbox on button click. seemingly random list of '4' items (picked out of nowhere) , seemingly useless textbox mean nothing.

putting aside , assuming have set of 4 business entities, each field on them user-editable, , action user can trigger, you'd this:

  • create viewmodel class represent item - eg myitemmodel
  • create viewmodel class represent set of items (likely return collection of first) - eg allmyitemslistmodel

then view:

  • create itemscontrol, itemssource bound instance of 'collection' of second viewmodel class
  • for each itemtemplate, have template or usercontrol each item
  • within template or usercontrol, bind textbox's text property appropriate property of first class
  • bind button's command property property on first class returning icommand - using relaycommand example

i don't know mean 'extracting texts nested viewmodels window viewmodel' - mean , why want it?

hope helps.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -