Send event to Host (Windows Form) from a WPF Control -Part 1
2013-06-17 23:01
507 查看
Send event to Host (Windows Form) from a WPF Control - Part 1
In this article you would see how event from WPF control could be passed to hosting windows form. To raise the event to back to host I am not creating the custom event, rather I would use standard .NET event handler. I
explained how to host a WPF control into the Windows Form in the one of my previous article.
Step1. Add Event Handler to Ok and Cancel Button:
Open WpfUserControlLib project in Visual Studio 2008.
Add the Event Handler for the btnOk and btnCancl button in the WPF Library
Add the public property in the UserControl1.xaml.cs file to share the value of the textbox with the host
Declare the Events for Ok and Cancel Buttons which can be subscribed by Windows Form.
Now add the code to the event handler so that we can raise the event to host also.
Step 2: Handle the WPF Control Event in Windows Form:
Add Handler to OKClick and Cancel click Event just after creating the instance of the user control.
Write code in the handler method. Here I user the UserName property in the OK button handler so show the how to share the values also.
Compile and Run the Windows Application you would see the WPF control is hosted in the Windows Control, Enter your name in the Text box and click OK and Cancel Button to see you are getting back the event from WPF control.
Summary: We can use the power of the WPF Control’s visual elements into the Windows Form application by hosting very easily. You can share the data and send the event to host from the WPF control. In Part
2 I would explain how to use custom arguments and custom events.
转自【http://www.a2zdotnet.com/View.aspx?Id=79#.Ub8fZZyvy3I】
In this article you would see how event from WPF control could be passed to hosting windows form. To raise the event to back to host I am not creating the custom event, rather I would use standard .NET event handler. I
explained how to host a WPF control into the Windows Form in the one of my previous article.
Step1. Add Event Handler to Ok and Cancel Button:
Open WpfUserControlLib project in Visual Studio 2008.
Add the Event Handler for the btnOk and btnCancl button in the WPF Library
private void btnOK_Click(object sender, RoutedEventArgs e) { } private void btnCancel_Click(object sender, RoutedEventArgs e) { }
Add the public property in the UserControl1.xaml.cs file to share the value of the textbox with the host
public string UserName { get { return txtName.Text; } set { txtName.Text = value; } }
Declare the Events for Ok and Cancel Buttons which can be subscribed by Windows Form.
public event EventHandler OkClick; public event EventHandler CancelClick;
Now add the code to the event handler so that we can raise the event to host also.
private void btnOK_Click(object sender, RoutedEventArgs e) { if (OkClick != null) OkClick(this, e); } private void btnCancel_Click(object sender, RoutedEventArgs e) { if (CancelClick != null) CancelClick(this, e); }
Step 2: Handle the WPF Control Event in Windows Form:
Add Handler to OKClick and Cancel click Event just after creating the instance of the user control.
_WPFUserControl.OkClick += new EventHandler(OnOkHandler); _WPFUserControl.CancelClick += new EventHandler(OnCancelHandler);
Write code in the handler method. Here I user the UserName property in the OK button handler so show the how to share the values also.
protected void OnOkHandler(object sender, EventArgs e) { MessageBox.Show("Hello: " +_WPFUserControl.UserName + " you clicked Ok Button"); } protected void OnCancelHandler(object sender, EventArgs e) { MessageBox.Show("you clicked Cancel Button"); }
Compile and Run the Windows Application you would see the WPF control is hosted in the Windows Control, Enter your name in the Text box and click OK and Cancel Button to see you are getting back the event from WPF control.
Summary: We can use the power of the WPF Control’s visual elements into the Windows Form application by hosting very easily. You can share the data and send the event to host from the WPF control. In Part
2 I would explain how to use custom arguments and custom events.
转自【http://www.a2zdotnet.com/View.aspx?Id=79#.Ub8fZZyvy3I】
相关文章推荐
- Send Custom Event to Host (Windows Form) from a WPF Control -Part 2
- how to reference the parent form from the WPF control(Control in ElementHost)
- OracleDBControl启动失败Unable to determine local host from URL REPOSITORY_URL=http://your-url.co 解决方法
- How to remove all event handlers from a control
- How to host WPF Control into the Windows Form Application
- Mac下showmount出错: Cannot retrieve info from host: 172.16.96.130: RPC failed:: RPC: Unable to send
- Raise event from user control to main page / Event delegation from user control to aspx page in ASP.
- OracleDBControl启动失败Unable to determine local host from URL REPOSITORY_URL=http://
- OracleDBControl启动失败Unable to determine local host from URL REPOSITORY_URL=http://your-url.co 解决方法
- 【转载】LINQ to SQL (Part 5 - Binding UI using the ASP:LinqDataSource Control)
- From MSI to WiX, Part 8 - Major Upgrade, by Alex Shevchuk
- 微信硬件蓝牙jsapi开发之sendDataToWXDevice接口和onReceiveDataFromWXDevice接口(11)
- 【MySQL】MySQL同步报错-> received end packet from server, apparent master shutdown: Slave I/O thread: Failed reading log event, reconnecting to retry报错解决和分析
- How to Build Your Own Blockchain Part 2 — Syncing Chains From Different Nodes
- How to obtain access to an ActiveX control from its property page 如何从一个控件的属性页中获取对该控件的访问?
- Nmap Cheat Sheet: From Discovery to Exploits – Part 1: Introduction to Nmap
- COM组件开发实践(五)---From C++ to COM :Part 2
- java.sql.SQLException: null, message from server: "Host 'xxx' is not allowed to connect
- 远程登录mysql,报错:message from server: "Host is not allowed to connect to this MySQL server"解决办法
- WPF:How to display a Bitmap on Image control