Shakti saxena
Shakti saxena

Reputation: 183

routing event to other user control

I need some assistance here.

I have got a button on my window. when button is clicked I want to route the action taken to the event already created in other usercontrol. These usercontrol is embedded in the window itself.

the event in usercontrol is

public void Item_Checked(object sender, RoutedEventArgs e)
        {
               object selected = this.GetType().GetField("Person", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).GetValue(this);
               this. PropertyGrid1.SelectedObject = selected;

        }

button as created in window is as :

<Button Content="clickme" Name="click" Grid.Column="2" Height="20" Width="50" Click="click_Click" />

button event in the window is as shown below:

 private void click_Click(object sender, RoutedEventArgs e)

        {
         /// dosomething   
        }

can someone help me here please?? I want to call Item_checked event on Button click.

Upvotes: 0

Views: 637

Answers (2)

Sheridan
Sheridan

Reputation: 69979

This is a common problem in programming. The generally accepted solution is simply to add a new method containing what is currently in your Item_Checked event handler and then to call that method from the handler and anywhere else you may want to:

In UserControl:

public void DoSomething()
{
    object selected = this.GetType().GetField("Person", 
        System.Reflection.BindingFlags.NonPublic | 
        System.Reflection.BindingFlags.Static).GetValue(this);
    this.PropertyGrid1.SelectedObject = selected;
}

public void Item_Checked(object sender, RoutedEventArgs e)
{
    DoSomething();
}

In Window (where yourUserControl is a reference to your UserControl):

private void click_Click(object sender, RoutedEventArgs e)
{
    yourUserControl.DoSomething();  
}

Upvotes: 1

Victory Jessie
Victory Jessie

Reputation: 679

Call with null value as RoutedEventArgs as follows

Item_Checked(PropertyGrid1,null);

Upvotes: 1

Related Questions