Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
End Sub
End Class
我不知道为什么在button_click后没有加载….. 是否很难从其他控件导航主窗口网格?
这只是猜测,因为你没有提供太多信息,但我注意到以下问题可能是罪魁祸首.
在您的第一个代码段中,您似乎是从MainForm创建员工:
Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)
您的以下评论似乎证实了这一假设:
This is from Window_Loaded event. Menu is User Control, and there I have few buttons to open and operate another user controls. When I press for example button “Question”
然而,在您的Employee类中,您正在创建MainWindow的全新实例,然后向其添加数据:
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
End Sub
End Class
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
mw.Show() ' <---
End Sub
End Class
您可能会看到弹出第二个表单,其中包含您在第一个表单上所期望的所有更改.
至于如何解决这个问题,最简单的方法是在初始化程序中添加一个参数(“Sub New”),它接受MainForm作为值.然后,您可以将值分配给字段或属性(可能只是您的mw字段)并继续您的快乐方式.不过,这会让你头疼不已,所以可能是开始学习更多关于software architecture的好时机,特别是separation of concerns的概念.
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
End Sub
End Class
我不知道为什么在button_click后没有加载….. 是否很难从其他控件导航主窗口网格?
这只是猜测,因为你没有提供太多信息,但我注意到以下问题可能是罪魁祸首.
在您的第一个代码段中,您似乎是从MainForm创建员工:
Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)
您的以下评论似乎证实了这一假设:
This is from Window_Loaded event. Menu is User Control, and there I have few buttons to open and operate another user controls. When I press for example button “Question”
然而,在您的Employee类中,您正在创建MainWindow的全新实例,然后向其添加数据:
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
End Sub
End Class
Public Class Employee
Dim mw As New MainWindow
Private Sub btnQuestionAdd_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles btnQuestionAdd.Click
Dim Que As New QuestionAdd
mw.MainGrid.Children.Add(Que)
Grid.SetRow(Que, 2)
Grid.SetColumn(Que, 1)
mw.Show() ' <---
End Sub
End Class
您可能会看到弹出第二个表单,其中包含您在第一个表单上所期望的所有更改.
至于如何解决这个问题,最简单的方法是在初始化程序中添加一个参数(“Sub New”),它接受MainForm作为值.然后,您可以将值分配给字段或属性(可能只是您的mw字段)并继续您的快乐方式.不过,这会让你头疼不已,所以可能是开始学习更多关于software architecture的好时机,特别是separation of concerns的概念.