wpf - CloseCommand implementation, multiple views, single model -


i have closecommand implementation closing views viewmodels (as discussed in several posts/blogs) working perfectly, puzzled (unable understand logic) behavior. (i have removed original lengthy code , replaced simplified version should able convey question trying ask)

in model (model1) have following parameters , properties

private action _closeaction; private icommand _closecommand; ..... public action closeaction { set { setproperty(ref _closeaction, value); } } public icommand closecommand { { return _closecommand; } } 

setproperty implemented in base class following..

protected bool setproperty<t>(ref t storage, t value, [callermembername] string propertyname = null) {     if(equals(storage, value))         return false;     storage = value;     this.onpropertychanged(propertyname);     return true; } 

in model constructor have initialized closecommand following

_closecommand = new relaycommand(param=>this._closeaction()); 

i have multiple command implementations in master model (modelx) create new views such following 2 (in each implementation, same model passed command parameter)...

private void showview1(object param) {     model1 model = param model1;     view1 view = new view1();     view.owner = application.current.mainwindow;     view.datacontext = model;     model.closeviewaction = new action(view.close);     view.show(); }  private void showview2(object param) {     model1 model = param model1;     view2 view = new view2();     view.owner = application.current.mainwindow;     view.datacontext = model;     model.closeviewaction = new action(view.close);     view.show(); }  , on multiple other views... 

on each view have close button have binding closecommand

command="{binding closeviewcommand}" 

now when there multiple views open using above method, if click on close button on view, particular view gets closed , no other.

now question is, while opening each new view, same single model's closeaction property being set new view's close function. so, shouldn't close command trigger close latest view, instead of correct view.

please me understand happening behind this.

thanks & regards

edit master model/view info

in master viewmodel have following commands..

private icommand _showview1command, _showview3command, _showview3command... 

in constructor

_showview1command = new relaycommand(param=>this.showview1(param)); _showview2command = new relaycommand(param=>this.showview2(param)); _showview3command = new relaycommand(param=>this.showview3(param)); ... 

properties

public icommand showview1command { {return this._showview1command;} } public icommand showview2command { {return this._showview2command;} } public icommand showview3command { {return this._showview3command;} } ... 

in master view have itemscontrol item datatemplate model1 containing following menu entries

<menuitem header="view1" commandparameter="{binding datacontext, relativesource={relativesource self}}" command="{binding path=datacontext.showview1command, relativesource={relativesource findancestor, ancestortype={x:type window}}}"/> <menuitem header="view2" commandparameter="{binding datacontext, relativesource={relativesource self}}" command="{binding path=datacontext.showview2command, relativesource={relativesource findancestor, ancestortype={x:type window}}}"/> <menuitem header="view3" commandparameter="{binding datacontext, relativesource={relativesource self}}" command="{binding path=datacontext.showview3command, relativesource={relativesource findancestor, ancestortype={x:type window}}}"/> .... 

you did create 2 close actions, each 1 view. when calling close action has reference view closing.

 model.closeviewaction = new action(view.close); 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -