c# - How to move WPF UserControl from one window to another? -
let's have usercontrol called myvideocontrol located in mainwindow.xaml:
<window name="_mainwindow"> <grid> <myvideocontrol name="_localvideo"/> </grid> </window>
now user clicks button , want usercontrol float on top of mainwindow.xaml, inside newly created window called popup.xaml.
<window name="_popupwindow"> <grid> <myvideocontrol name="_localvideo"/> </grid> </window>
how accomplish this, entire object gets moved? use xaml declaratively place myvideocontrol inside windows, i'm guessing i'll need programatically?
yes can removing usercontrol
mainwindow
, adding logical child of control in popupwin
window.
usercontrol.xaml:
<usercontrol x:class="wpfapplication1.usercontrol1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" mc:ignorable="d" d:designheight="100" d:designwidth="100"> <grid> <textbox x:name="txtblock1" text="hai"/> </grid> </usercontrol>
mainwindow.xaml :
<window x:class="wpfapplication1.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:wpfapplication1="clr-namespace:wpfapplication1" title="mainwindow" height="550" width="555"> <grid> <stackpanel x:name="mainpanel" orientation="vertical "> <button content="button" height="23" name="button1" width="75" click="button1_click" /> <wpfapplication1:usercontrol1 x:name="myusercontrol" /> </stackpanel> </grid> </window>
popupwin.xaml :
<window x:class="wpfapplication1.popupwin" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="popupwin" height="300" width="300"> <stackpanel x:name="mainpanel"/> </window>
popupwin.xaml.cs: expose new constructor accept usercontrol
, add child mainpanel
public partial class popupwin : window { public popupwin() { initializecomponent(); } private usercontrol control; public popupwin(usercontrol control) : this() { this.control = control; this.mainpanel.children.add(this.control); } }
mainwindow.xaml.cs on button_click remove usercontrol current mainwindow
, pass popupwin
, in case via constructor.
private void button1_click(object sender, routedeventargs e) { this.mainpanel.children.remove(this.myusercontrol); var wind = new popupwin(this.myusercontrol); wind.showdialog(); }
note: usercontrol
instance should logical child of one
element @ anytime.
Comments
Post a Comment