Click here to Skip to main content
15,895,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have a WPF aplication in c# i have two usercontrol and the mainwindows, my first user control is usercontrol1 that hold my menu with one button and the event click, and i got a grid on my mainwindows name uscholder to load the usercontrol2 that i send from the event click of the button on my usercontrol1

What I have tried:

public  partial class UserControl1 : UserControl
    {
        public UserControl1()
        {
            InitializeComponent();

            
        }
       
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            UserControl2 usc = new UserControl2();
            MainWindow maingrid = new MainWindow();

            if (maingrid.uscholder != null)
            {
                maingrid.uscholder.Children.Clear();
                maingrid.uscholder.Children.Add(usc);
            }
            else
            {
                maingrid.uscholder.Children.Add(usc);
            }


        }
    }
Posted
Updated 26-Jun-20 20:54pm

See here: Transferring information between two forms, Part 2: Child to Parent[^] - it's based around forms, but it's exactly the same process for controls.

You create an event in UserControl1 which the MainWindow handles. When the user clicks the button in UserControl1, you raise the event and the MainWindow Handler does the work with UserControl2.
 
Share this answer
 
Insted of grid in this case consider using ContentControl
<ContentControl Name="CC"/>

and for c#
CC.Content = new UserControl2();
 
Share this answer
 
So you have MainWindow with UserControl1, and after pressing button on uc1 you open another MW with usc1 and replace it with usc2 in your newly opened window?

private void Button_Click(object sender, RoutedEventArgs e)
{
    MainWindow maingrid = Application.Current.MainWindow as MainWindow;

    if (maingrid.uscholder.Children.Count > 0)
        maingrid.uscholder.Children.Clear();

    maingrid.uscholder.Children.Add(new UserControl2());
}


is this what you want ?
 
Share this answer
 
v8

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900