如何将WPF UserControl从一个窗口移动到另一个窗口?

Ete*_*l21 4 c# wpf user-controls window

假设我在MainWindow.xaml中有一个名为MyVideoControl的UserControl:

<Window Name="_mainWindow">
    <Grid>
        <MyVideoControl Name="_localVideo"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

现在,用户单击一个按钮,我希望UserControl在一个名为PopUp.xaml的新创建的窗口内浮动在MainWindow.xaml之上.

<Window Name="_popUpWindow">
    <Grid>
        <MyVideoControl Name="_localVideo"/>
    </Grid>
</Window>
Run Code Online (Sandbox Code Playgroud)

我如何实现这一目标,以便整个对象被移动?目前我使用XAML声明性地将MyVideoControl放在我的窗口中,但我猜我需要以编程方式完成所有操作?

jac*_*ous 7

是的,你可以通过删除做到这一点userControlMainwindow并将其添加为逻辑孩子任何的控件的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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

MainWindow.xaml.cs On Button_Click从当前删除userControlMainWindow并将其传递给PopupWin,在这种情况下通过构造函数.

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        this.mainPanel.Children.Remove(this.myUserControl);

        var wind = new PopupWin(this.myUserControl);

        wind.ShowDialog();
    }
Run Code Online (Sandbox Code Playgroud)

注:userControl实例应该永远只有一个合乎逻辑的子one元素在任何时候.