Call a method in a parent from a child user control - VB.NET/WPF

Is it possible to call a method from a loaded user control?

MainWindow.xaml.vb loads user control Login.Xaml.vb.  Login.Xaml.vb has a button on it.  When that button is clicked, I want the method MainWindow.LoadUC(UserControl) to run.

This is my MainWindow.Xaml code:
Imports System.IO
Imports Microsoft.PointOfService
Imports System.Data
Imports System.Data.SqlClient
Imports System.Data.Sql
Imports System.Net.NetworkInformation

Class MainWindow
    Public Sub MainWindow()

        InitializeComponent()
    End Sub

    Public Sub Window_Loaded()
        LoadLogin()
    End Sub

    Public Sub Load_Login()

    End Sub

    Public Sub btnAdmin_Click()

    End Sub

    Public Sub btnLogOut_Click()

    End Sub

    Public Sub LoadUC(ByVal userControl As UserControl)
        mainPanel.Children.Clear()
        mainPanel.Children.Add(userControl)
    End Sub

    Private Sub LoadLogin()
        Dim LoginScreen As New Login
        'Content = LoginScreen

        mainPanel.Children.Add(LoginScreen)

    End Sub

End Class

Open in new window


And the Login User Control code is:
Public Class Login
    Public uc As UserControl


    Public Sub numberclick()
        MsgBox("Number Click")
    End Sub

    Public Sub Login()

    End Sub
    Public Sub enterclick() Handles btnZero_Copy9.Click
        MsgBox("Number Click")
    End Sub
    Public Sub clearclick()
        MsgBox("Number Click")
        uc = New UC_Text
        
        'Not sure how to send "uc" back to MainWindow.
        'This is what I want to do, but doesn't work:
        MainWindow.LoadUC(uc)

    End Sub
End Class

Open in new window


Maybe there's a better way to go about it.  Basically, I need to load various user controls, and put them in the MainWindow's area, replacing the User Control that was there before.

Thanks for your help!!!
LVL 1
slightlyoffAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chinmay PatelChief Technical NinjaCommented:
You can pass MainWindow's reference to your user control and then call the method as if it was any other method.
0
slightlyoffAuthor Commented:
Thanks - what would be the best way to do that?
 
In MainWindow code:
Dim LoginScreen As New Login
LoginScreen.mwindow = MainWindow

Open in new window


And then in Login
Public mwindow as Page

Open in new window


I'm new to WPF - I've been doing WinForms too long...  sorry if it's a silly question.

Thanks again for the quick reply!

Steve
0
Dustin SaundersDirector of OperationsCommented:
You can create a property on the child form of the type of the parent, then refer back to the parent directly (is what he means).  See this simple example:
public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Form2 form2 = new Form2 { ReferringForm = this };
        }

        public void DoSomethingElse(string variable)
        {
            //do something.
        }
    }

Open in new window


public partial class Form2 : Form
    {
        public Form1 ReferringForm { get; set; }

        public Form2()
        {
            InitializeComponent();
        }

        private void DoSomething()
        {
            ReferringForm.DoSomethingElse("something");
        }
    }

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
slightlyoffAuthor Commented:
Ok - I'll give it a try.  I'm assuming, other thank vb.net markup, the winform and wpf function close to the same here?
I won't get to try it until later tonight, but I'll let you know.

Thanks for the help!
0
Dustin SaundersDirector of OperationsCommented:
Appropriate answers for the questions- weighted due to the time taken to write example code.
1
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WPF

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.