* Reset branch for WPF changes * Convert BMP to PNG; fix link-out-of-scope err * Add snippets for WPF... 6794 files!!!! * Add missing snippets * update file updated between migration * Fix paths to include * update breadcrumb and toc * fix index links * fix index links * fix index links * fix markdown
16 KiB
title, titleSuffix, ms.date, dev_langs, helpviewer_keywords, ms.assetid
| title | titleSuffix | ms.date | dev_langs | helpviewer_keywords | ms.assetid | ||||
|---|---|---|---|---|---|---|---|---|---|
| Arrange Windows Forms controls in WPF | 04/03/2018 |
|
|
a1db8049-15c7-45d6-ae3d-36a6735cb848 |
Walkthrough: Arranging Windows Forms Controls in WPF
This walkthrough shows you how to use [!INCLUDETLA2#tla_winclient] layout features to arrange Windows Forms controls in a hybrid application.
Tasks illustrated in this walkthrough include:
- Creating the project.
- Using default layout settings.
- Sizing to content.
- Using absolute positioning.
- Specifying size explicitly.
- Setting layout properties.
- Understanding z-order limitations.
- Docking.
- Setting visibility.
- Hosting a control that does not stretch.
- Scaling.
- Rotating.
- Setting padding and margins.
- Using dynamic layout containers.
For a complete code listing of the tasks illustrated in this walkthrough, see Arranging Windows Forms Controls in WPF Sample.
When you are finished, you will have an understanding of Windows Forms layout features in [!INCLUDETLA2#tla_winclient]-based applications.
Prerequisites
You need Visual Studio to complete this walkthrough.
Creating the Project
To create and set up the project, follow these steps:
-
Create a WPF Application project named
WpfLayoutHostingWf. -
In Solution Explorer, add references to the following assemblies:
- WindowsFormsIntegration
- System.Windows.Forms
- System.Drawing
-
Double-click MainWindow.xaml to open it in XAML view.
-
In the xref:System.Windows.Window element, add the following Windows Forms namespace mapping.
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" -
In the xref:System.Windows.Controls.Grid element set the xref:System.Windows.Controls.Grid.ShowGridLines%2A property to
trueand define five rows and three columns.[!code-xamlWpfLayoutHostingWfWithXaml#2]
Using Default Layout Settings
By default, the xref:System.Windows.Forms.Integration.WindowsFormsHost element handles the layout for the hosted Windows Forms control.
To use default layout settings, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#3]
-
Press F5 to build and run the application. The Windows Forms xref:System.Windows.Forms.Button?displayProperty=nameWithType control appears in the xref:System.Windows.Controls.Canvas. The hosted control is sized based on its content, and the xref:System.Windows.Forms.Integration.WindowsFormsHost element is sized to accommodate the hosted control.
Sizing to Content
The xref:System.Windows.Forms.Integration.WindowsFormsHost element ensures that the hosted control is sized to display its content properly.
To size to content, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#4]
-
Press F5 to build and run the application. The two new button controls are sized to display the longer text string and larger font size properly, and the xref:System.Windows.Forms.Integration.WindowsFormsHost elements are resized to accommodate the hosted controls.
Using Absolute Positioning
You can use absolute positioning to place the xref:System.Windows.Forms.Integration.WindowsFormsHost element anywhere in the user interface (UI).
To use absolute positioning, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#5]
-
Press F5 to build and run the application. The xref:System.Windows.Forms.Integration.WindowsFormsHost element is placed 20 pixels from the top side of the grid cell and 20 pixels from the left.
Specifying Size Explicitly
You can specify the size of the xref:System.Windows.Forms.Integration.WindowsFormsHost element using the xref:System.Windows.FrameworkElement.Width%2A and xref:System.Windows.FrameworkElement.Height%2A properties.
To specify size explicitly, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#6]
-
Press F5 to build and run the application. The xref:System.Windows.Forms.Integration.WindowsFormsHost element is set to a size of 50 pixels wide by 70 pixels high, which is smaller than the default layout settings. The content of the Windows Forms control is rearranged accordingly.
Setting Layout Properties
Always set layout-related properties on the hosted control by using the properties of the xref:System.Windows.Forms.Integration.WindowsFormsHost element. Setting layout properties directly on the hosted control will yield unintended results.
Setting layout-related properties on the hosted control in [!INCLUDETLA2#tla_xaml] has no effect.
To see the effects of setting properties on the hosted control, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#7]
-
In Solution Explorer, double-click MainWindow.xaml.vb or MainWindow.xaml.cs to open it in the Code Editor.
-
Copy the following code into the
MainWindowclass definition:[!code-csharpWpfLayoutHostingWfWithXaml#101] [!code-vbWpfLayoutHostingWfWithXaml#101]
-
Press F5 to build and run the application.
-
Click the Click me button. The
button1_Clickevent handler sets the xref:System.Windows.Forms.Control.Top%2A and xref:System.Windows.Forms.Control.Left%2A properties on the hosted control. This causes the hosted control to be repositioned within the xref:System.Windows.Forms.Integration.WindowsFormsHost element. The host maintains the same screen area, but the hosted control is clipped. Instead, the hosted control should always fill the xref:System.Windows.Forms.Integration.WindowsFormsHost element.
Understanding Z-Order Limitations
Visible xref:System.Windows.Forms.Integration.WindowsFormsHost elements are always drawn on top of other WPF elements, and they are unaffected by z-order. To see this z-order behavior, do the following:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#8]
-
Press F5 to build and run the application. The xref:System.Windows.Forms.Integration.WindowsFormsHost element is painted over the label element.
Docking
xref:System.Windows.Forms.Integration.WindowsFormsHost element supports [!INCLUDETLA2#tla_winclient] docking. Set the xref:System.Windows.Controls.DockPanel.Dock%2A attached property to dock the hosted control in a xref:System.Windows.Controls.DockPanel element.
To dock a hosted control, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#9]
-
Press F5 to build and run the application. The xref:System.Windows.Forms.Integration.WindowsFormsHost element is docked to the right side of the xref:System.Windows.Controls.DockPanel element.
Setting Visibility
You can make your Windows Forms control invisible or collapse it by setting the xref:System.Windows.UIElement.Visibility%2A property on the xref:System.Windows.Forms.Integration.WindowsFormsHost element. When a control is invisible, it is not displayed, but it occupies layout space. When a control is collapsed, it is not displayed, nor does it occupy layout space.
To set the visibility of a hosted control, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#10]
-
In MainWindow.xaml.vb or MainWindow.xaml.cs, copy the following code into the class definition:
[!code-csharpWpfLayoutHostingWfWithXaml#102] [!code-vbWpfLayoutHostingWfWithXaml#102]
-
Press F5 to build and run the application.
-
Click the Click to make invisible button to make the xref:System.Windows.Forms.Integration.WindowsFormsHost element invisible.
-
Click the Click to collapse button to hide the xref:System.Windows.Forms.Integration.WindowsFormsHost element from the layout entirely. When the Windows Forms control is collapsed, the surrounding elements are rearranged to occupy its space.
Hosting a Control That Does Not Stretch
Some Windows Forms controls have a fixed size and do not stretch to fill available space in the layout. For example, the xref:System.Windows.Forms.MonthCalendar control displays a month in a fixed space.
To host a control that does not stretch, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#11]
-
Press F5 to build and run the application. The xref:System.Windows.Forms.Integration.WindowsFormsHost element is centered in the grid row, but it is not stretched to fill the available space. If the window is large enough, you may see two or more months displayed by the hosted xref:System.Windows.Forms.MonthCalendar control, but these are centered in the row. The [!INCLUDETLA2#tla_winclient] layout engine centers elements that cannot be sized to fill the available space.
Scaling
Unlike WPF elements, most Windows Forms controls are not continuously scalable. To provide custom scaling, you override the xref:System.Windows.Forms.Integration.WindowsFormsHost.ScaleChild%2A?displayProperty=nameWithType method.
To scale a hosted control by using the default behavior, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#12]
-
Press F5 to build and run the application. The hosted control and its surrounding elements are scaled by a factor of 0.5. However, the hosted control's font is not scaled.
Rotating
Unlike WPF elements, Windows Forms controls do not support rotation. The xref:System.Windows.Forms.Integration.WindowsFormsHost element does not rotate with other WPF elements when a rotation transformation is applied. Any rotation value other than 180 degrees raises the xref:System.Windows.Forms.Integration.WindowsFormsHost.LayoutError event.
To see the effect of rotation in a hybrid application, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#13]
-
Press F5 to build and run the application. The hosted control is not rotated, but its surrounding elements are rotated by an angle of 180 degrees. You may have to resize the window to see the elements.
Setting Padding and Margins
Padding and margins in [!INCLUDETLA2#tla_winclient] layout are similar to padding and margins in Windows Forms. Simply set the xref:System.Windows.Controls.Control.Padding%2A and xref:System.Windows.FrameworkElement.Margin%2A properties on the xref:System.Windows.Forms.Integration.WindowsFormsHost element.
To set padding and margins for a hosted control, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#14] [!code-xamlWpfLayoutHostingWfWithXaml#15]
-
Press F5 to build and run the application. The padding and margin settings are applied to the hosted Windows Forms controls in the same way they would be applied in Windows Forms.
Using Dynamic Layout Containers
Windows Forms provides two dynamic layout containers, xref:System.Windows.Forms.FlowLayoutPanel and xref:System.Windows.Forms.TableLayoutPanel. You can also use these containers in [!INCLUDETLA2#tla_winclient] layouts.
To use a dynamic layout container, follow these steps:
-
Copy the following XAML into the xref:System.Windows.Controls.Grid element:
[!code-xamlWpfLayoutHostingWfWithXaml#16]
-
In MainWindow.xaml.vb or MainWindow.xaml.cs, copy the following code into the class definition:
[!code-csharpWpfLayoutHostingWfWithXaml#103] [!code-vbWpfLayoutHostingWfWithXaml#103]
-
Add a call to the
InitializeFlowLayoutPanelmethod in the constructor:[!code-csharpWpfLayoutHostingWfWithXaml#104] [!code-vbWpfLayoutHostingWfWithXaml#104]
-
Press F5 to build and run the application. The xref:System.Windows.Forms.Integration.WindowsFormsHost element fills the xref:System.Windows.Controls.DockPanel, and xref:System.Windows.Forms.FlowLayoutPanel arranges its child controls in the default xref:System.Windows.Forms.FlowLayoutPanel.FlowDirection%2A.
See also
- xref:System.Windows.Forms.Integration.ElementHost
- xref:System.Windows.Forms.Integration.WindowsFormsHost
- Design XAML in Visual Studio
- Layout Considerations for the WindowsFormsHost Element
- Arranging Windows Forms Controls in WPF Sample
- Walkthrough: Hosting a Windows Forms Composite Control in WPF
- Walkthrough: Hosting a WPF Composite Control in Windows Forms