简体   繁体   English

Winforms主持WPF控制事件

[英]Winforms host WPF control events

I'm trying to host a wpf control (gridview) on my winform. 我正在尝试在Winform上托管wpf控件(gridview)。

I'm using elementHost to create the wpf control on my winform. 我正在使用elementHost在winform上创建wpf控件。

How do I create an event whenever I want to add rows to my wpf control? 每当我想向wpf控件添加行时如何创建事件?

Wrap your gridview in an usercontrol and handle the events in the usercontrol. 在用户控件中包装gridview并处理该用户控件中的事件。 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 请注意,当您托管wpf控件时,某些事件将不会被调用。对我有用的解决方案:在加载时和elementhost获得焦点时将焦点设置为用户控件

To subscribe event of the WPF control in Winforms is same with other events. 在Winforms中订阅WPF控件的事件与其他事件相同。 Just get the WPF control instance and use the code as: 只需获取WPF控件实例并将代码用作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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