简体   繁体   中英

Winforms host WPF control events

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?

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

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");
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM