Send Custom Event to host (Windows form) from a WPF control-Part 2

Source: Internet
Author: User

Send Custom Event to host (Windows form) from a WPF control-Part 2

 

In the previous articles we have seen how to host
WPF control into the Windows Form and send
Standard events to Windows form from WPF control. In this article we wocould see how custom event from WPF control cocould be passed to hosting windows form.

 

There are 3 steps involve to do achieve this.

 

Step 1: Create the Custom Event and Its argument:

To create any custom argument for the event it has to be derived from EventArgs. These arguments have to be available to the source and receivers so it has to be declared in the library project.

 

  • Create the class library project and name itCustomevents.
  • Rename the class1 file to Controller
  • Add the CustomArgs class and derive it from EventArgs as shown here.
    public class CustomArgs : EventArgs    {        private string _userName;        public CustomArgs(string userName)        {            this._userName = userName;        }        // This is a straightforward implementation for        // declaring a public read only field        public string UserName        {            get            {                return this._userName;            }        } }
  • Add the Custom delegate and its Event
///<summary>/// CustomMsgEventHandler Delegate type defining the prototype of the callback method that receivers must implement.///</summary>public delegate void CustomMsgEventHandler(            Object sender, CustomArgs args); ///<summary>/// Event EventCustomMsg, which the subscriber would listen and source would raise this event///</summary>public event CustomMsgEventHandler EventCustomMsg;
  • Add the Public method used by the source who wocould raise the event
public void NewCustomMsg(Object sender, CustomArgs e)        {            // Has any objects registered interest with our event?            if (EventCustomMsg != null)            {                // Yes, notify all the objects                EventCustomMsg(sender, e);            }     }

 

Step2. Create the WPF control project to raise the custom event to windows Form Application:

  • Create WpfUserControlLib project in Visual Studio 2008 described in the use
    WPF control in Windows Form.
  • Add the reference ofCustomeventsProject created in the first step.
  • Create the class member of the Controller class
private WPFCustomEvents.Controller _controller;
  • Add the constructor and initialize the controller class object. Same Controller object has to shared by all source and target classes. Its better create the singleton object of Controller class.
public UserControl1(WPFCustomEvents.Controller controller){     InitializeComponent();     _controller = controller;}

 

  • 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 cancel button, which can be subscribed by Windows form. For the OK button events already declared in the Controller class

 

public event EventHandler CancelClick;
  • Now add the code to the event handler so that we can raise the event to host also.

In the OK button event create the custom argument and fills all the members and raise the custom event.

private void btnOK_Click(object sender, RoutedEventArgs e){WPFCustomEvents.Controller.CustomArgs eCustomArg = new WPFCustomEvents.Controller.CustomArgs(txtName.Text);_controller.NewCustomMsg(this, eCustomArg);}private void btnCancel_Click(object sender, RoutedEventArgs e){            if (CancelClick != null)                CancelClick(this, e);}

 

Step 3: handle the WPF control's Custom Event in Windows form:

  • Create winformwpfhostapp windows form project in Visual Studio 2008 as described in the article
    Use the WPF control in Windows form.
  • Add the reference ofWpfusercontrollibAndCustomevents
    Projects.
  • In the constructor create the object of the controller class and pass it to wpfusercontrollib's WPF control.
public Form1(){   InitializeComponent();   _controller = new WPFCustomEvents.Controller();  elementHost1.Dock = DockStyle.Fill;   _WPFUserControl = new WpfUserControlLib.UserControl1(_controller);   _controller.EventCustomMsg += new WPFCustomEvents.Controller.CustomMsgEventHandler(OnCustomMsg);   _WPFUserControl.CancelClick += new EventHandler(OnCancelHandler);   elementHost1.Child = _WPFUserControl;}
  • 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. If you see in oncustommsg I am getting all the information along with Custom Event and argument.

 

protected void OnCustomMsg(object sender, WPFCustomEvents.Controller.CustomArgs e){MessageBox.Show("Hello: " + e.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 wocould 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 including the Custom Event and custom
Arument on OK button.

 

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 (both custom and existing) to host from the WPF control.

From [http://www.a2zdotnet.com/View.aspx? Id = 94 #. ub8htjyvy3i]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.