0

I have a Winforms app (class named Form_WinForms for the example) and I want to add an elementhost control to it in order to host a WPF Usercontrol (named Form_WPF).

But I don't know how to handle the delegates between them: I want to exchange with the Form_WPF controls from the Form_WinForms and vice versa. How could I do ? Thanks

In the example, I just want to modify the text of the control textbox (WPF technologie) by clicking inside a Winforms button.

In Winforms class:

// In Form_WinForms (my main form)

private void btn_Debug_Click(object sender, EventArgs e)
{
    // Form_WPF.GetInstance().Invoke(new ChangeTextBox_WPF(Form_WPF._ChangeTextBox_WPF), Form_WPF.GetInstance().TextBoxDebug, "test"); // what I would have done if Form_WPF was a WinForms form 
    Form_WPF.GetInstance().Dispatcher.Invoke(new ChangeTextBox_WPF(Form_WPF._ChangeTextBox_WPF), Form_WPF.GetInstance().TextBoxDebug, "test");
}

In WPF class:

// In the Usercontrol Form_WPF (which is hosted by an elementhost in Winforms)

private delegate void ChangeTextBox_WPF(System.Windows.Controls.TextBox TextBox, String texte);
public static void _ChangeTextBox_WPF(System.Windows.Controls.TextBox TextBox, String texte) { TextBox.Text = texte; TextBox.Tag = texte; }

private static Form_WPF _Form_WPF;

public static Form_WPF GetInstance()
{
    if (_Form_WPF == null)
    {
        _Form_WPF = new Form_WPF();
    }
    return _Form_WPF;
}
PLB
  • 197
  • 1
  • 10

1 Answers1

1

Cast the Child property of the ElementHost to your WPF control type, e.g.:

Form_WPF wpfControl = elementHost.Host as Form_WPF;

Then you can access any members of the hosted instance as usual.

mm8
  • 163,881
  • 10
  • 57
  • 88
  • Hi, the `child` property of my elementhost was already to Form_WPF, the delegate function `_ChangeTextBox_WPF` is triggering when I press the Winforms debug button, but the text of the WPF textbox is not changing – PLB Feb 14 '20 at 14:02
  • Probably because you set the `Text` property of the wrong instance. Remove all static stuff. You don't need it. – mm8 Feb 14 '20 at 14:04
  • If I remove it I have the followings error on the line `Form_WPF.GetInstance().Dispatcher.Invoke(new ChangeTextBox_WPF(Form_WPF._ChangeTextBox_WPF), Form_WPF.GetInstance().TextBoxDebug, "test");` : "An object reference is required for the non-static field, method, or property 'Form_WPF.GetInstance()" – PLB Feb 14 '20 at 14:11
  • Remove that line an try my sample code? Again, you should cast the `Child` property of the `ElementHost` control that hosts the WPF control. – mm8 Feb 14 '20 at 14:11
  • Sorry I'm kind of new with elementhosts and WPF. Where should I set this line (`Form_WPF wpfControl = elementHost.Host as Form_WPF; `)? – PLB Feb 14 '20 at 14:15
  • Wherever you want to access the WPF control in your WinForms app. – mm8 Feb 14 '20 at 14:16
  • Ok so I have removed all the statics fields, and this is my btn_debug_click event: `Form_WPF wpfControl = elementHost1.Child as Form_WPF; wpfControl.Dispatcher.Invoke(new ChangeTextBox_WPF(wpfControl._ChangeTextBox_WPF), wpfControl.TextBoxDebug, "test");` But no change in the WPF textbox – PLB Feb 14 '20 at 14:26
  • Wait hold on, this is working! My element host was hosted both in my main winforms forms and another winforms forms, and I was checking the other winforms forms. But with inside the main winforms, this is changing. Thanks @mm8 ! – PLB Feb 14 '20 at 14:30
  • So last question, how could I do if I have multiple elementhost and all of them have the same hosted WPF usercontrol and I want them to be the same ? Cast the child property to each elementhost to the WPF, and copy each time the code same lines ? – PLB Feb 14 '20 at 14:45
  • 1
    Please ask a new question if you have another issue. Avoid extended discussions in comments. – mm8 Feb 14 '20 at 14:46