0

I'm trying to host a wpf control (gridview) on my winform.

I'm using elementHost to create the wpf control on my winform.

How do I create an event whenever I want to add rows to my wpf control?

tzush
  • 1
  • 2

2 Answers2

0

Wrap your gridview in an usercontrol and handle the events in the usercontrol. Notice that some events will be not called when you are hosting a wpf control.Solution that works for me: set the focus to your usercontrol when loaded and when elementhost got focus

Bohdan Biehov
  • 431
  • 4
  • 15
0

To subscribe event of the WPF control in Winforms is same with other events. Just get the WPF control instance and use the code as:

wpfbutton1.Click += new RoutedEventHandler(wpfbutton1_Click);     

void wpfbutton1_Click(object sender, RoutedEventArgs e)
{
   throw new NotImplementedException();
}

Sample code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        ElementHost host = new ElementHost() { Dock = DockStyle.Fill };
        this.Controls.Add(host);

        System.Windows.Controls.Button wpfButton = 
            new System.Windows.Controls.Button() { Content = "WPF Button" };
        host.Child = wpfButton;

        wpfButton.Click += new         System.Windows.RoutedEventHandler(wpfButton_Click);
    }

    void wpfButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
        MessageBox.Show("Button is clicked");
    }
}
StepUp
  • 36,391
  • 15
  • 88
  • 148