如何从一个窗口移动用户控件WPF到另一个? [英] How to move WPF UserControl from one window to another?

查看:282
本文介绍了如何从一个窗口移动用户控件WPF到另一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个用户控件称为MyVideoControl位于MainWindow.xaml:

Let's say I have a UserControl called MyVideoControl located in MainWindow.xaml:

<Window Name="_mainWindow">
    <Grid>
        <MyVideoControl Name="_localVideo"/>
    </Grid>
</Window>

现在用户点击一个按钮,我想浮在MainWindow.xaml顶部的用户控件, 。里面叫PopUp.xaml一个新创建的窗口

Now a user clicks a button and I want the UserControl to float on top of the MainWindow.xaml, inside a newly created window called PopUp.xaml.

<Window Name="_popUpWindow">
    <Grid>
        <MyVideoControl Name="_localVideo"/>
    </Grid>
</Window>



我如何做到这一点,所以整个对象被感动?目前我使用XAML来声明放在里面MyVideoControl我的窗户,但我猜我需要编程做的一切?

How do I accomplish this, so the entire object gets moved? Currently I use XAML to declaratively place MyVideoControl inside my windows, but I'm guessing I'll need to do everything programatically?

推荐答案

是的,你可以通过删除用户控件主窗口做到这一点,将其添加为一个逻辑孩子任何的控制在 PopupWin 窗口。

Yes you can do this by removing the userControl from the Mainwindow and adding it as a logical child to any of the control in the PopupWin window.

UserControl.xaml:

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:

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:

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:揭露一个新的构造接受用户控件并将其添加为一个孩子的的mainPanel

PopupWin.xaml.cs: Expose a new constructor to accept userControl and add it as a child to the 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从目前的主窗口删除用户控件,并把它传递给 PopupWin ,在这种情况下,通过构造。

MainWindow.xaml.cs On Button_Click remove the userControl from the current MainWindow and pass it to PopupWin, in this 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: The userControl instance should always be a logical child of only one element at anytime.

这篇关于如何从一个窗口移动用户控件WPF到另一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆