Open user control from user controls from the main window grid WPF

Maybe first I will talk about my application.
For example, when an employee logs in to my application, he will load the “Employee Menu”:

< p>

Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

This is from the Window_Loaded event The menu is a user control, I have several buttons to open and operate another user control. When I press the button “Question” for example:

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

Me I don’t know why it is not loaded after button_click…..
Is it difficult to navigate the main window grid from other controls?

This is just a guess, because you did not provide much information, but I noticed that the following problem may be The culprit.

In your first snippet, you seem to be creating employees from MainForm:

Dim Empl As New Employee 
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

Your following comment seems to confirm this assumption:

< /p>

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”

However, in your Employee class, you are creating a brand new instance of MainWindow and then adding data to it:

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

If this observation is correct Yes, then I think you need to go back to the book and understand the concepts of classes and instances.

You basically created a second form (hidden because you never showed it explicitly), and then modified the second form instead of the original form. To prove this hypothesis, try adding the following code Line:

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

You may see a All the changes expected on a form.

As for how to solve this problem, the easiest way is to add a parameter (“Sub New”) in the initializer, which accepts MainForm as the value. Then, You can assign values ​​to fields or attributes (probably just your mw field) and continue your happy way. However, this will give you a headache, so it may be a good time to start learning more about software architecture, especially separation The concept of concerns.

Maybe first I will talk about my application.
For example, when an employee logs in to my application, he will load the “employee menu” :

Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

This is from the Window_Loaded event. The menu is a user control, and I have several buttons to open and operate another user control. When I press the button “Question” for example:

Public Class Em ployee
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

< p>I don’t know why it is not loaded after button_click…..
Is it difficult to navigate the main window grid from other controls?

This is just a guess, because you didn’t provide much information, but I noticed that the following problem may be the culprit.

In your first code snippet, you seem to be creating employees from MainForm:

Dim Empl As New Employee
MainGrid.Children.Add(Empl)
Grid.SetRow(Empl, 1)

Your following comment seems to confirm this hypothesis:

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”

However, in your Employee class , You are creating a brand new instance of MainWindow, and then adding data to it:

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

If this observation is correct, then I think you need to go back to the book and understand The concept of classes and instances.

You basically create a second form (hidden because you never explicitly displayed it), and then modify the second form instead of the original form. To prove this hypothesis , Please try Try adding the following lines of code:

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

You may see a second form pop up, where Include all the changes you expect on the first form.

As for how to solve this problem, the easiest way is to add a parameter (“Sub New”) in the initializer, which accepts MainForm as Value. Then you can assign a value to a field or attribute (probably just your mw field) and continue your happy way. However, this will give you a headache, so it may be a good time to start learning more about software architecture , Especially the concept of separation of concerns.

Leave a Comment

Your email address will not be published.