2013-04-09 173 views
4

比方說,我有一個用戶控件稱爲MyVideoControl位於MainWindow.xaml:如何將WPF UserControl從一個窗口移動到另一個窗口?

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

現在,用戶點擊一個按鈕,我想浮在MainWindow.xaml頂部的用戶控件,被稱爲新創建的窗口內PopUp.xaml。

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

我該如何做到這一點,所以整個對象被移動?目前我使用XAML來聲明式地將MyVideoControl放置在我的窗口中,但我猜我需要以編程方式做所有事情?

+0

我不認爲這是可能的。你在其他地方看過這個功能嗎?我很確定所有的控件都必須包含在一個(也是唯一的一個)窗口中。 – Jras 2013-04-09 01:46:08

+1

您可以簡單地從用戶窗口中刪除用戶控件,並在用戶單擊按鈕時將其添加到新窗口。 – KF2 2013-04-09 02:09:33

+0

@Jras - 也許我不清楚。在任何給定時間**,控件都將被包含在一個(並且只有一個)Window **中。所以在某一點上它將是MainWindow,但在另一個點上它將是PopUpWindow。只有一個實例,但它在兩個窗口中移動。 – Eternal21 2013-04-09 02:15:16

回答

7

是的,您可以通過從Mainwindow中刪除userControl並將它作爲邏輯子項添加到PopupWin窗口中的任何控件中來執行此操作。

enter image description here

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元素的邏輯子隨時。

+1

接受這個答案,因爲它仍然能夠使用XAML,而且不必訴諸於資源字典,只需很少的代碼。 – Eternal21 2013-04-09 13:52:21

+0

@永恆21:很高興知道它幫助.. – 2013-04-10 02:10:39

1

如果您製作UserControl資源,您可以執行此操作。

實施例:

的App.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> 

結果:

enter image description hereenter image description here

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); 
    } 
} 

}

我沒有在那裏做的唯一的事情是增加的能力來移動當在彈出窗口中單擊時,按鈕回到主窗口。

相關問題