w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Open User Control from user control in main window grid WPF

This is just a guess as you've not provided much information, but I noticed the following issue which may be the culprit.

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

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

Your following comment would seem to confirm that assumption:

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"

And yet, 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 books and understand the concept of classes and instances.

You essentially created a second form (which is hidden because you never explicitly show it) and then modify this second form rather than the original. To prove this hypothesis, try adding the following line 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'll likely see a second form pop up with all of the changes your were expecting on your first form.

As to how to fix this, the easiest path is going to be adding a parameter to your initializer ("Sub New") which accepts the MainForm as a value. You can then assign the value to a field or property (probably just your mw field) and continue on your merry way. This is going to give you headaches down the road, though, so it might be a good time to start learning more about software architecture, especially the concept of separation of concerns.





© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.