是的,您可以通過從Mainwindow
中刪除userControl
並將它作爲邏輯子項添加到PopupWin
窗口中的任何控件中來執行此操作。
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:露出新的構造函數接受userControl
,將其作爲孩子添加到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在Button_Click從目前MainWindow
刪除用戶控件,並把它傳遞給PopupWin
,在這種情況下通過構造函數。
private void button1_Click(object sender, RoutedEventArgs e)
{
this.mainPanel.Children.Remove(this.myUserControl);
var wind = new PopupWin(this.myUserControl);
wind.ShowDialog();
}
注:的userControl
實例應該永遠只有one
元素的邏輯子隨時。
我不認爲這是可能的。你在其他地方看過這個功能嗎?我很確定所有的控件都必須包含在一個(也是唯一的一個)窗口中。 – Jras 2013-04-09 01:46:08
您可以簡單地從用戶窗口中刪除用戶控件,並在用戶單擊按鈕時將其添加到新窗口。 – KF2 2013-04-09 02:09:33
@Jras - 也許我不清楚。在任何給定時間**,控件都將被包含在一個(並且只有一個)Window **中。所以在某一點上它將是MainWindow,但在另一個點上它將是PopUpWindow。只有一個實例,但它在兩個窗口中移動。 – Eternal21 2013-04-09 02:15:16