[英]Add UserControl in MainWindow from another UserControl
我在UserControl_1中有两个UserControls,有一个按钮可将UserControl_2添加到MainWindow.axml中的StackPanel中。 我在UserControl_1中执行以下操作:
private void Button_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
UserControl_2 uc = new UserControl_2();
((MainWindow)Application.Current.MainWindow).stackpanel_2.Children.Add(uc);
}
如何在MVVM模式中执行此操作?
简短的答案是,您不使用MVVM执行此操作。
在MVVM中,您的视图是视图模型中包含的数据的直观表示。 您永远不会直接添加控件,因为它不会代表任何数据!
如果您的View上有一个ItemsControl
绑定到View Model中的一个集合,那么向该集合添加一个项目将添加适当的控件。 这就是您将如何执行现有代码在MVVM中的精神。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.