Privacy policy. WPF and Windows Forms present two different architectures for creating application interfaces. The System. Integration namespace provides classes that enable common interoperation scenarios. This topic describes which interoperation scenarios are supported and which scenarios are not supported. Special consideration is given to the hybrid control scenario. A hybrid control has a control from one technology nested in a control from the other technology.
This is also called a nested interoperation. A multilevel hybrid control has more than one level of hybrid control nesting. An example of a multilevel nested interoperation is a Windows Forms control that contains a WPF control, which contains another Windows Forms control. Multilevel hybrid controls are not supported. The following interoperation scenarios are supported when a WPF control hosts a Windows Forms control:.
The following list describes the known limitations when the WindowsFormsHost element attempts to integrate its hosted Windows Forms control into the WPF layout system. In some cases, Windows Forms controls cannot be resized, or can be sized only to specific dimensions. For example, a Windows Forms ComboBox control supports only a single height, which is defined by the control's font size.
In a WPF dynamic layout, which assumes that elements can stretch vertically, a hosted ComboBox control will not stretch as expected. Windows Forms controls cannot be rotated or skewed. For example, when you rotate your user interface by 90 degrees, hosted Windows Forms controls will maintain their upright position.
In most cases, Windows Forms controls do not support proportional scaling. Although the overall dimensions of the control will scale, child controls and component elements of the control may not resize as expected. This limitation depends on how well each Windows Forms control supports scaling. As a result, this Windows Forms control is rendered as part of the WPF window, and you can communicate with the control from the application. The code-behind file, MainWindow.
The primary tasks are:. Modifying various properties of MyControl1 , based on how the collection of option buttons are set. The initialization code is contained in an event handler for the window's Loaded event and attaches an event handler to the control's OnButtonClick event.
In MainWindow. You can then use that reference to attach an event handler to OnButtonClick. In addition to providing a reference to the control itself, WindowsFormsHost exposes a number of the control's properties, which you can manipulate from the application.
The initialization code assigns those values to private global variables for later use in the application. So that you can easily access the types in the MyControls DLL, add the following Imports or using statement to the top of the file.
MyControl1 raises the OnButtonClick event when the user clicks either of the control's buttons. The data in the text boxes is packed into the MyControlEventArgs object.
If the user clicks the OK button, the event handler extracts the data and displays it in the panel below MyControl1. The WindowsFormsHost element exposes several of the hosted control's default properties. As a result, you can change the appearance of the control to match the style of your application more closely.
The sets of option buttons in the left panel enable the user to modify several color and font properties. Each set of buttons has a handler for the Click event, which detects the user's option button selections and changes the corresponding property on the control.
Build and run the application. Add some text in the Windows Forms composite control and then click OK. The text appears in the labels. Click the different radio buttons to see the effect on the control.
Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Contents Exit focus mode. Please rate your experience Yes No. Any additional feedback? Submit and view feedback for This product This page. 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. Click the different radio buttons to see the effect on the WPF composite control.
Skip to main content. This browser is no longer supported. Download Microsoft Edge More info.
0コメント