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:
- 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.
- 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.
- 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,itemssourcebound instance of 'collection' of second viewmodel class - for each
itemtemplate, have template orusercontroleach item - within template or
usercontrol, bindtextbox's text property appropriate property of first class - bind
button'scommandproperty property on first class returningicommand- usingrelaycommandexample
i don't know mean 'extracting texts nested viewmodels window viewmodel' - mean , why want it?
hope helps.
Comments
Post a Comment