c# - WPF Reference User Control in MainWindowViewModel Code -
user control located in controls\tabcontrol. contains tabcontrol 2 tabitems (ruletab , diagramtab).
in mainwindowviewmodel class have:
private void showsavedialog() { system.windows.forms.savefiledialog sfd = new system.windows.forms.savefiledialog(); sfd.filter = "text file (*.txt)|*.txt"; bool? saveresult = sfd.showdialog() == system.windows.forms.dialogresult.ok; if (saveresult == true) { string s = sfd.filename; filepath = s; savefile(s); settitle(sfd.filename); ruletab.header = new system.io.fileinfo(sfd.filename).name; ruletab.focus(); }
ruletab (tabitem) isn't being found because it's in user control. how reference in class?
tabcontrol.xaml
x:name="tabeditor" x:fieldmodifier="public" mc:ignorable="d" d:designheight="300" d:designwidth="423"> <tabcontrol x:name="tabcontrol" x:fieldmodifier="public" width="auto" padding="0" margin="3" datacontext="{binding}"> <local:closeabletabitem header="diagram" x:name="diagramtab" x:fieldmodifier="public" height="25"> <image height="auto" x:name="resultimage" x:fieldmodifier="public" stretch="fill" width="auto" /> </local:closeabletabitem> <local:closeabletabitem header="rulebase" x:name="ruletab" x:fieldmodifier="public" height="25" > <grid> <textbox height="auto" x:name="ruletext" x:fieldmodifier="public" width="auto" text="" acceptstab="true" acceptsreturn="true" verticalscrollbarvisibility="auto" horizontalscrollbarvisibility="visible" /> </grid> </local:closeabletabitem> </tabcontrol>
there various possibilities if @ code-example think add event filesaved
viewmodel , raise event after sucessfull saving.
mainwindow can register event , further processing such focus desired control.
setting header however, declare property in viewmodel , bind ruletabs header it. can set header in event, has unnecessary drawbacks.
update
desired here short example implementing event , property in viewmodel. assume viewmodel implements inotifypropertychanged. if not derives dependencyobject, declare read depencencyproperty filename
instead of clr-property:
string m_filename; public event eventhandler filesaved; private void showsavedialog() { system.windows.forms.savefiledialog sfd = new system.windows.forms.savefiledialog(); sfd.filter = "text file (*.txt)|*.txt"; bool? saveresult = sfd.showdialog() == system.windows.forms.dialogresult.ok; if (saveresult == true) { string s = sfd.filename; filepath = s; savefile(s); filename=sfd.filename; onfilesaved(eventargs.empty); } } protected virtual void onfilesaved(eventargs e){ if(null != filesaved){ filesaved(this,e); } } public string filename{ get{return m_filename;} private set{ if(value!=m_filename){ m_filename=value; onpropertychanged(new propertychangedeventargs("filename")); } } }
Comments
Post a Comment