4

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

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

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

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

我该如何做到这一点,所以整个对象都被移动了?目前我使用 XAML 以声明方式将 MyVideoControl 放置在我的窗口中,但我猜我需要以编程方式完成所有操作?

4

3 回答 3

9

是的,您可以通过从userControl中删除Mainwindow并将其作为逻辑子项添加到PopupWin窗口中的任何控件来执行此操作。

在此处输入图像描述

用户控件.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 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();
    }

注意:实例userControl都应该是唯一one元素的逻辑子元素。

于 2013-04-09T03:21:46.553 回答
1

如果您制作UserControl资源,您可以执行此操作。

例子:

应用程序.Xaml

<Application x:Class="WpfApplication10.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             StartupUri="MainWindow.xaml">
    <Application.Resources>
        <ContentControl x:Key="sharedContent">
            <Label Content="StackOverFlow" />
        </ContentControl>
    </Application.Resources>
</Application>

主窗口

<Window x:Class="WpfApplication10.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="325" Width="422" Name="UI">

    <StackPanel>
        <Button Content="Open PopUp" Click="Button_Click" />
        <ContentControl Content="{DynamicResource sharedContent}" />
    </StackPanel>
</Window>

弹出窗口

<Window x:Class="WpfApplication10.PopupWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="PopupWindow" Height="300" Width="300" xmlns:my="clr-namespace:WpfApplication10">
    <Grid>
        <ContentControl Content="{DynamicResource sharedContent}" />
    </Grid>
</Window>

结果:

在此处输入图像描述 在此处输入图像描述

于 2013-04-09T02:43:05.037 回答
1

另一种方法可以实现,使用动态布局......

 public partial class MainWindow : Window
{
    public Button ControlToMove { get; set; }

    public MainWindow()
    {
        InitializeComponent();

        //create button
        ControlToMove = new Button();

        //add text to button
        ControlToMove.Content = "Click to move me to pop up window";

        //add a new routed event to the buttons click property
        ControlToMove.Click += new RoutedEventHandler(MoveControlToPopUp);

        //add control to the layout grid
        MainGrid.Children.Add(ControlToMove);
    }

    //This method moves the button to a popup window
    private void MoveControlToPopUp(object sender, RoutedEventArgs e)
    {
        //get the name of the control from sender
        var control = sender as Button;
        var controlName = control.Name;

        //checks to see if this is the control we want moved
        //if its not, method exits
        if (controlName != ControlToMove.Name) return;

        //create copy of the control
        var copiedControl = control;

        //remove control from existing window
        MainGrid.Children.Remove(control);

        //create pop up window
        var popUpWindow = new PopUpWindow(copiedControl);
        popUpWindow.Show();
    }
}

public class PopUpWindow : Window
{
    public Grid Layout { get; set; }
    public PopUpWindow(Button button)
    {
        //create a grid for the new window
        Layout = new Grid();

        //add control to grid
        Layout.Children.Add(button);

        //add grid to window
        this.AddChild(Layout);
    }
}

}

我没有做的唯一一件事是添加在弹出窗口中单击按钮时将按钮移回主窗口的功能。

于 2013-04-09T03:04:58.510 回答