假设我在MainWindow.xaml中有一个名为MyVideoControl的UserControl:
<Window Name="_mainWindow">
<Grid>
<MyVideoControl Name="_localVideo"/>
</Grid>
</Window>
现在用户点击一个按钮,我希望UserControl在一个名为PopUp.xaml的新创建的窗口内浮动在MainWindow.xaml之上。
<Window Name="_popUpWindow">
<Grid>
<MyVideoControl Name="_localVideo"/>
</Grid>
</Window>
如何实现这一目标,以便移动整个对象?目前我使用XAML声明性地将MyVideoControl放在我的窗口中,但我猜我是否需要以编程方式完成所有操作?
答案 0 :(得分:7)
是的,您可以通过从userControl
移除Mainwindow
并将其作为逻辑子项添加到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 On Button_Click从当前MainWindow
中删除userControl并将其传递给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 :(得分: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>
PopUp 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>
结果:
答案 2 :(得分: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);
}
}
}
我唯一没做的就是在弹出窗口中单击按钮时添加将按钮移回主窗口的功能。