Host wpf control in windows forms




















As we can see in the XAML above, we define a combo-box with customer as an item source. The combo-box has an item template for presenting the customer details in a "grid-like" style:.

In WPF, it is easily done using three labels and binding their context into the " Customer " class properties. This step is actually easier. The second section discusses in detail how to host the composite control in a WPF application, receive events from the control, and access some of the control's properties. The Windows Forms composite control used in this example is a simple data-entry form.

This form takes the user's name and address and then uses a custom event to return that information to the host. The following illustration shows the rendered control. For the location, specify a conveniently named top-level folder, such as WpfHostingWindowsFormsControl.

Later, you will put the host application in this folder. Click OK to create the project. The default project contains a single control named UserControl1. Your project should have references to the following system DLLs. If any of these DLLs are not included by default, add them to the project. Add five Label controls and their corresponding TextBox controls, sized and arranged as they are in the preceding illustration, on the form.

In the example, the TextBox controls are named:. Add two Button controls labeled OK and Cancel. In the example, the button names are btnOK and btnCancel , respectively. Open the form in code view. The control returns the collected data to its host by raising the custom OnButtonClick event. The data is contained in the event argument object. The following code shows the event and delegate declaration.

The only difference between the two handlers is the event argument's IsOK property. This property enables the host to determine which button was clicked. It is set to true for the OK button, and false for the Cancel button. The following code shows the two button handlers. For this assembly to be referenced by a WPF application, it must have a strong name.

To create a strong name, create a key file with Sn. Open a Visual Studio command prompt. This launches a console window with customized environment variables. To include the key file in your project, right-click the project name in Solution Explorer and then click Properties.

In the Project Designer, click the Signing tab, select the Sign the assembly check box and then browse to your key file. The implementation is straightforward, and is not discussed further. The application handles the OnButtonClick event to receive the data from the composite control.

The application also has a set of option buttons that you can use to modify the control's appearance. The following illustration shows the application. Click the Browse tab, and browse to the folder that contains MyControls. In the upper-right corner of the form, add a System. Panel control to hold the WPF composite control. Add the following System. GroupBox controls to the form. RadioButton controls to the System. GroupBox controls. Label controls to the last System.

These controls display the data returned by the WPF composite control. You generally implement the hosting code in the form's Load event handler. The following code shows the Load event handler, a handler for the WPF composite control's Loaded event, and declarations for several global variables that are used later. In the Windows Forms Designer, double-click the form to create a Load event handler. At the top of Form1. Create a new ElementHost object.

Set the control's Dock property to DockStyle. Add the ElementHost control to the Panel control's Controls collection. Host the composite control on the form by assigning the control to the ElementHost control's Child property. OnButtonClick is a custom event that is fired by the composite control when the user clicks the OK or Cancel button. You handle the event to get the user's response and to collect any data that the user specified.

Loaded is a standard event that is raised by a WPF control when it is fully loaded. The event is used here because the example needs to initialize several global variables using properties from the control. At the time of the form's Load event, the control is not fully loaded and those values are still set to null. You need to wait until the control's Loaded event occurs before you can access those properties. The Loaded event handler is shown in the preceding code.

The OnButtonClick handler is discussed in the next section. The event handler checks the event argument's IsOK field to determine which button was clicked. The lbl data variables correspond to the Label controls that were discussed earlier. If the user clicks the OK button, the data from the control's TextBox controls is assigned to the corresponding Label control.

If the user clicks Cancel , the Text values are set to the default strings. Build and run the application. The text appears in the labels.

At this point, code has not been added to handle the radio buttons. The RadioButton controls on the form will enable the user to change the WPF composite control's foreground and background colors as well as several font properties. The background color is exposed by the ElementHost object.

The remaining properties are exposed as custom properties of the control. Double-click each RadioButton control on the form to create CheckedChanged event handlers. Replace the CheckedChanged event handlers with the following code.



0コメント

  • 1000 / 1000