Click here to Skip to main content
11,484,640 members (60,633 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# WPF User-controls
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 Smile | :)
Posted 2-Jan-12 23:12pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hello,

Use delegate in your User Control

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

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:

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.
  Permalink  
v3
Comments
Amir Mahfoozi at 3-Jan-12 5:41am
   
+5 Good answer.
Shahin Khorshidnia at 3-Jan-12 5:52am
   
Thank you Amir jan.
csprogrammer at 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 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Thank you your answers were really helpful Big Grin | :-D
  Permalink  
Comments
Shahin Khorshidnia at 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)

  Print Answers RSS
0 Maciej Los 190
1 Sergey Alexandrovich Kryukov 180
2 Raje_ 155
3 OriginalGriff 147
4 Mehdi Gholam 120
0 Sergey Alexandrovich Kryukov 8,484
1 OriginalGriff 7,948
2 Sascha Lefèvre 3,199
3 Maciej Los 2,681
4 Richard Deeming 2,370


Advertise | Privacy | Mobile
Web01 | 2.8.150520.1 | Last Updated 4 Jan 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100