c# - WPF: Popout existing usercontrol -
imagine next situation: have application window several usercontrols inside. displayed side side in past, want show 1 of them in popup window. not in popup control new window.
see example xaml:
<window x:class="wpfapplication3.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfapplication3="clr-namespace:wpfapplication3" title="mainwindow" height="350" width="525"> <grid> <wpfapplication3:usercontrol1 visibility="hidden" x:name="usercontrol1"/> <button click="buttonbase_onclick" width="100" height="60">open window</button> </grid>
in code behind need deattach usercontrol current window , assign new one:
private void buttonbase_onclick(object sender, routedeventargs e) { var parent = visualtreehelper.getparent(usercontrol1); if (parent != null) { parent.removechild(usercontrol1); } var w = new window { content = usercontrol1, title = "sample", sizetocontent = sizetocontent.widthandheight, resizemode = resizemode.canresize }; w.show(); }
and after calling w.show() getting blank white window.
if in button click handler change
content = usercontrol1
to
content = new usercontrol1()
i right content well. can't use way because want keep usercontrol state during pop-out , pop-in events. how can show in new window existing usercontrol without recreating it?
i not sure how calling removechild on dependencyobject method doesn't seem exist. note visualtreehelper.getparent returns dependencyobject so, code posted should not compile unless have extension method somewhere defining removechild.
in case want cast parent object type grid or panel , remove usercontrol children property, set usercontrol content of window.
private void buttonbase_onclick(object sender, routedeventargs e) { grid parent = visualtreehelper.getparent(usercontrol1) grid; if (parent != null) { parent.children.remove(usercontrol1); } var w = new window { content = usercontrol1, title = "sample", sizetocontent = sizetocontent.widthandheight, resizemode = resizemode.canresize }; w.show(); }
Comments
Post a Comment