Click here to Skip to main content
15,885,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
explanation of the application:

I have a main wpf windows containing "add_button" and "view-list" button and a "content_grid" .When the user clicks the "add_button" the control containing the add form is displayed in the "content_grid" , the same when he clicks view list he gets the usercontrol containing the list + add button.

The problem that i met:

To do that i have a method in my main_window show_control(control c) that gets the control and displays it in the "content_grid". the problem is that i want to display the "add_control" from the "list_control" (the user clicks "add_btn" that is in the "list_control" to display "add_control").


Thank you in advance :)
Posted

Hello,

Use delegate in your User Control

For example you got a button named "Add" on your control:

C#
public partial class Test : UserControl
{
    public Test()
    {
        InitializeComponent();
    }

    public delegate void RunMethodHandler(object sender);
    public event RunMethodHandler RunMethod;

    private void Add_Click(object sender, RoutedEventArgs e)
    {
        if (RunMethod != null)
            RunMethod(null);
    }
}



Then in your MainWindow, Add the UserControl to MainWindow. I did and renamed it to "MyControl", then:

C#
public MainWindow()
{
    InitializeComponent();

    MyControl.RunMethod +=new Test.RunMethodHandler(TestMethod);
}

public void TestMethod(object sender)
{
    MessageBox.Show("Ok");
}


At last, when you Click on Add, the TestMethod() will run.

Good luck.
 
Share this answer
 
v3
Comments
Amir Mahfoozi 3-Jan-12 5:41am    
+5 Good answer.
Shahin Khorshidnia 3-Jan-12 5:52am    
Thank you Amir jan.
csprogrammer 3-Jan-12 6:20am    
Thank you for the reply but I'm using
content_grid.Children.Add(UserControl);
to Add the UserControl to main_Window.
Shahin Khorshidnia 3-Jan-12 9:52am    
Ok, Do not add the handler in Constructor (MainMenu.)

Add it after: content_grid.Children.Add(UserControl);For example:

content_grid.Children.Add(UserControl);

UserControl.RunMethod +=new Test.RunMethodHandler(TestMethod);

If any more question you can ask.
Thank you your answers were really helpful :D
 
Share this answer
 
Comments
Shahin Khorshidnia 4-Jan-12 6:16am    
Your welcome ;)

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