2
votes

I have a winform with a wpf usercontrol on it (ElementHost1). The usercontrol contains only a button. How can I know when the wpf button has been clicked in my winform? How can I "redirect" the events from wpf usercontrol to winform?

Thanks.

2

2 Answers

3
votes

This link might be helpful to you.

Or a simple event handling in VB.NET

Public Event ClickMe()

Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
    RaiseEvent ClickMe()
End Sub

Then in your actual window you can have this:

Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    AddHandler SampleClick1.ClickMe, AddressOf Sample_Click
End Sub

Private Sub Sample_Click()
    MessageBox.Show("This is a proof!")
End Sub

That SampleClick1 variable is from the designer code generated available to the form for your use.

Friend WithEvents ElementHost1 As System.Windows.Forms.Integration.ElementHost
Friend SampleClick1 As WindowsApplication1.SampleClick
1
votes

Here is the one solution i found

in UserControl1.Xaml.cs

public static RoutedEvent ChkBoxChecked = EventManager.RegisterRoutedEvent("CbChecked", RoutingStrategy.Bubble,
                                                                typeof(RoutedEventHandler), typeof(CheckBox));
public event RoutedEventHandler CbChecked
{
    add
    {
        AddHandler(ChkBoxChecked, value);
    }
    remove
    {
        RemoveHandler(ChkBoxChecked, value);
    }
}

private void cbTreeView_Checked(object sender, RoutedEventArgs e)
{
    RoutedEventArgs args = new RoutedEventArgs(ChkBoxChecked);
    RaiseEvent(args);                
}

Now in MainForm Form1 shown event we can add CbChecked event

private void Form1_Shown(object sender, EventArgs e)
{
    this.elemetHost1.CbChecked += new System.Windows.RoutedEventHandler(wpfusercontrol_CbChecked);
    //elementHost1 is the name of wpf usercontrol hosted in Winform
}

void elementHost1_CbChecked(object sender, System.Windows.RoutedEventArgs e)
{
    //This event will raise when user clicks on chekbox
}

i am facing an issue here.i am rasing the same event in Form1 for all checkbox clickevents in UserControl1.so i want to know which checkbox is clicked in the mainform.i tried to see in the RoutedEventArgs e....but doesnt help how to know which checkbox is clicked in the mainform