Click here to Skip to main content
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 OriginalGriff 304
1 Sergey Alexandrovich Kryukov 255
2 Maciej Los 250
3 Shweta N Mishra 216
4 PIEBALDconsult 174
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,586
3 Manas Bhardwaj 4,946
4 Maciej Los 4,665


Advertise | Privacy | Mobile
Web03 | 2.8.1411023.1 | Last Updated 4 Jan 2012
Copyright © CodeProject, 1999-2014
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