The following sections describe specific aspects of layout behavior when hosting Windows Forms controls in WPF.
Scaling, Unit Conversion, and Device Independence
Whenever the WindowsFormsHost element performs operations involving WPF and Windows Forms dimensions, two coordinate systems are involved: device-independent pixels for WPF and hardware pixels for Windows Forms. Therefore, you must apply proper unit and scaling conversions to achieve a consistent layout.
Conversion between the coordinate systems depends on the current device resolution and any layout or rendering transforms applied to the WindowsFormsHost element or to its ancestors.
If the output device is 96 dpi and no scaling has been applied to the WindowsFormsHost element, one device-independent pixel is equal to one hardware pixel.
All other cases require coordinate system scaling. The hosted control is not resized. Instead, the WindowsFormsHost element attempts to scale the hosted control and all of its child controls. Because Windows Forms does not fully support scaling, the WindowsFormsHost element scales to the degree supported by particular controls.
Override the ScaleChild method to provide custom scaling behavior for the hosted Windows Forms control.
In addition to scaling, the WindowsFormsHost element handles rounding and overflow cases as described in the following table.
Conversion issue
|
Description
|
|---|
Rounding
|
WPF device-independent pixel dimensions are specified as double, and Windows Forms hardware pixel dimensions are specified as int. In cases where double-based dimensions are converted to int-based dimensions, the WindowsFormsHost element uses standard rounding, so that fractional values less than 0.5 are rounded down to 0.
|
Overflow
|
When the WindowsFormsHost element converts from double values to int values, overflow is possible. Values that are larger than MaxValue are set to MaxValue.
|
Layout-related Properties
Layout Changes in the Hosted Control
Layout changes in the hosted Windows Forms control are propagated to WPF to trigger layout updates. The InvalidateMeasure method on WindowsFormsHost ensures that layout changes in the hosted control cause the WPF layout engine to run.
Continuously Sized Windows Forms Controls
Windows Forms controls that support continuous scaling fully interact with the WPF layout system. The WindowsFormsHost element uses the MeasureOverride and ArrangeOverride methods as usual to size and arrange the hosted Windows Forms control.
Sizing Algorithm
The WindowsFormsHost element uses the following procedure to size the hosted control:
The WindowsFormsHost element overrides the MeasureOverride and ArrangeOverride methods.
To determine the size of the hosted control, the MeasureOverride method calls the hosted control's GetPreferredSize method with a constraint translated from the constraint passed to the MeasureOverride method.
The ArrangeOverride method attempts to set the hosted control to the given size constraint.
If the hosted control's Size property matches the specified constraint, the hosted control is sized to the constraint.
If the Size property does not match the specified constraint, the hosted control does not support continuous sizing. For example, the MonthCalendar control allows only discrete sizes. The permitted sizes for this control consist of integers (representing the number of months) for both height and width. In cases such as this, the WindowsFormsHost element behaves as follows:
If the Size property returns a larger size than the specified constraint, the WindowsFormsHost element clips the hosted control. Height and width are handled separately, so the hosted control may be clipped in either direction.
If the Size property returns a smaller size than the specified constraint, WindowsFormsHost accepts this size value and returns the value to the WPF layout system.