mvvm - WPF: Changing tabs makes Windowsformshost child disappear -
if can spare time, working on problem can't find solution on internet.
i need 2 tabs' richtextboxes bind same property. both richtextboxes hosted in wpf via windowsformshost. if alternate between tabs, 1 richttextbox dissapear (always first 1 visible). migrating app , far, forced use windowsforms richtextbox.
i hope managed convey problem - sorry, not native speaker.
thanks in advance
edit: asked provide clear example of problem. note. rewrote question. further, have uploaded micro app have isolated problem. click 2 tab buttons alternately , 1 richtextbox dissapear.
below, provide code if serves:
this mainwindow (xaml):
<stackpanel orientation="horizontal" height="35" margin="0,35,0,0" verticalalignment="top" > <button x:name="tab1" command="{binding leftcommand}" content="left" minwidth="100" /> <button x:name="tab2" command="{binding rightcommand}" content="right" minwidth="100" /> </stackpanel> <frame x:name="myframe" content="{binding path=currenttab, mode=twoway, updatesourcetrigger=propertychanged}" margin="5,70,0,0" navigationuivisibility="hidden" />
this viewmodel:
class mainwindowviewmodel : inotifypropertychanged { public icommand leftcommand { get; } public icommand rightcommand { get; } private tabviewmodel mytabviewmodel { get; set; } private pageleft mypageleft { get; set; } private pageright mypageright { get; set; } public mainwindowviewmodel() { this.leftcommand = new modelcommand(p => this.setselectedtab("left")); this.rightcommand = new modelcommand(p => this.setselectedtab("right")); this.mytabviewmodel = new tabviewmodel(); this.mypageleft = new pageleft() { datacontext = this.mytabviewmodel }; this.mypageright = new pageright() { datacontext = this.mytabviewmodel }; //initial view on //this.setselectedtab("left"); } private void setselectedtab(string param) { switch (param) { case "left": this.currenttab = this.mypageleft; break; case "right": this.currenttab = this.mypageright; break; } } private object _currenttab; public object currenttab { { return _currenttab; } set { if (value != _currenttab) { _currenttab = value; notifypropertychanged_mainviewmodel(); } } } public event propertychangedeventhandler propertychanged; // method called set accessor of each property. // callermembername attribute applied optional propertyname // parameter causes property name of caller substituted argument. private void notifypropertychanged_mainviewmodel([callermembername] string propertyname = "") { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
furthermore, have 2 pages (mypageleft, mypageright) use same viewmodel (tabviewmodel) , use same bit of xaml code:
<contentcontrol content="{binding path=mywindowsformshost, mode=twoway, updatesourcetrigger=propertychanged}" />
both pages use same tabviewmodel:
class tabviewmodel : inotifypropertychanged { private windowsformshost _mywindowsformshost; public windowsformshost mywindowsformshost { { return _mywindowsformshost; } set { if (value != _mywindowsformshost) { _mywindowsformshost = value; notifypropertychanged_tabviewmodel(); } } } public tabviewmodel() { this.mywindowsformshost = new windowsformshost() { child = new richtextbox() { text = "test" } }; } public event propertychangedeventhandler propertychanged; // method called set accessor of each property. // callermembername attribute applied optional propertyname // parameter causes property name of caller substituted argument. private void notifypropertychanged_tabviewmodel([callermembername] string propertyname = "") { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } }
the problem: if start app , click on 2 tab buttons alternatingly, 1 of framed richtextboxes dissapear.
if might need it, used dirty solution - although might not recommendable.
i extended event of switch tab buttons. takes rtf property of selected tab's richtextbox , infuses in other richtextbox. goes kinda this:
if (tab2 button clicked) this.myrtf = tab1.richtextbox.rtf; tab2.richttextbox.rtf = this.myrtf;
note beginner's hack on overall questionable approach.
thanks read question!
Comments
Post a Comment