演练:在 WPF 中排列 Windows 窗体控件

更新:2010 年 8 月

本演练演示如何使用 WPF 布局功能在混合应用程序中排列 Windows Forms控件。

本演练涉及以下任务:

  • 创建项目。

  • 使用默认布局设置。

  • 根据内容调整大小。

  • 使用绝对定位。

  • 显式指定大小。

  • 设置布局属性。

  • 了解 Z 顺序限制。

  • 停靠。

  • 设置可见性。

  • 承载不拉伸的控件。

  • 缩放。

  • 旋转。

  • 设置空白和边距。

  • 使用动态布局容器。

有关本演练中所阐释任务的完整代码清单,请参见 Arranging Windows Forms Controls in WPF Sample(在 WPF 中排列 Windows 窗体控件示例)。

在完成本演练后,您将对基于 WPF 的应用程序中的 Windows Forms布局功能有一定了解。

系统必备

您需要以下组件来完成本演练:

  • Visual Studio 2010.

创建项目

创建和设置项目

  1. 创建一个名为 WpfLayoutHostingWf 的 WPF 应用程序项目。

  2. 在解决方案资源管理器中,添加对以下程序集的引用。

    • WindowsFormsIntegration

    • System.Windows.Forms

    • System.Drawing

  3. 双击 MainWindow.xaml 将其在 XAML 视图中打开。

  4. Window 元素中,添加以下 Windows Forms命名空间映射。

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    
  5. Grid 元素中,将 ShowGridLines 属性设置为 true,并定义五行和三列。

    <Grid ShowGridLines="true">
      <Grid.RowDefinitions>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
    
      <Grid.ColumnDefinitions>
        <ColumnDefinition/>
        <ColumnDefinition/>
        <ColumnDefinition/>
      </Grid.ColumnDefinitions>
    

使用默认布局设置

默认情况下,WindowsFormsHost 元素处理所承载的 Windows Forms控件的布局。

使用默认布局设置

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Default layout. -->
    <Canvas Grid.Row="0" Grid.Column="0">
      <WindowsFormsHost Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 按 F5 生成并运行该应用程序。 此时 Windows Forms System.Windows.Forms.Button 控件将出现在 Canvas 中。 所承载的控件根据其内容调整大小,并且 WindowsFormsHost 元素也会根据所承载的控件调整大小。

根据内容调整大小

WindowsFormsHost 元素确保调整所承载的控件的大小,以正确地显示其内容。

根据内容调整大小

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Sizing to content. -->
    <Canvas Grid.Row="1" Grid.Column="0">
      <WindowsFormsHost Background="Orange">
        <wf:Button Text="Windows Forms control with more content" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
    <Canvas Grid.Row="2" Grid.Column="0">
      <WindowsFormsHost FontSize="24" Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 按 F5 生成并运行该应用程序。 两个新按钮控件会调整大小以正确地显示较长的文本字符串和较大的字号,并且 WindowsFormsHost 元素也会根据所承载的控件调整大小。

使用绝对定位

您可以使用绝对定位将 WindowsFormsHost 元素放在用户界面 (UI) 中的任意位置。

使用绝对定位

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Absolute positioning. -->
    <Canvas Grid.Row="3" Grid.Column="0">
      <WindowsFormsHost Canvas.Top="20" Canvas.Left="20" Background="Yellow">
        <wf:Button Text="Windows Forms control with absolute positioning" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 按 F5 生成并运行该应用程序。 WindowsFormsHost 元素置于网格单元格中距离顶部 20 像素、距离左侧 20 像素的位置。

显式指定大小

可以使用 WidthHeight 属性指定 WindowsFormsHost 元素的大小。

显式指定大小

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Explicit sizing. -->
    <Canvas Grid.Row="4" Grid.Column="0">
      <WindowsFormsHost Width="50" Height="70" Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 按 F5 生成并运行该应用程序。 WindowsFormsHost 元素的大小设置为 50 像素宽、70 像素高,这比默认布局设置小。 Windows Forms控件的内容相应地重新排列。

设置布局属性

应始终使用 WindowsFormsHost 元素的属性对所承载的控件设置与布局有关的属性。 直接对所承载的控件设置布局属性会产生意外的结果。

以 XAML 对所承载的控件设置与布局有关的属性无效。

查看对所承载的控件设置属性的效果

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Setting hosted control properties directly. -->
    <Canvas Grid.Row="0" Grid.Column="1">
      <WindowsFormsHost Width="160" Height="50" Background="Yellow">
        <wf:Button Name="button1" Click="button1_Click" Text="Click me" FlatStyle="Flat" BackColor="Green"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 在“解决方案资源管理器”中,双击 MainWindow.xaml. vb 或 MainWindow.xaml.cs 以在代码编辑器中将其打开。

  3. 将下面的代码复制到 MainWindow 类定义中。

    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim b As System.Windows.Forms.Button = sender
    
        b.Top = 20
        b.Left = 20
    
    End Sub
    
    private void button1_Click(object sender, EventArgs e )
    {
        System.Windows.Forms.Button b = sender as System.Windows.Forms.Button;
    
        b.Top = 20;
        b.Left = 20;
    }
    
  4. 按 F5 生成并运行该应用程序。

  5. 单击**“Click me”(单击这里)**按钮。 button1_Click 事件处理程序设置所承载的控件的 TopLeft 属性。 这导致所承载的控件在 WindowsFormsHost 元素中重新定位。 宿主占据相同的屏幕区域,但所承载的控件被剪辑。 所承载的控件应总是填充 WindowsFormsHost 元素。

了解 Z 顺序限制

可见的 WindowsFormsHost 元素始终绘制在其他 WPF 元素的上面,它们不受 Z 顺序的影响。

查看 Z 顺序限制

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Z-order demonstration. -->
    <Canvas Grid.Row="1" Grid.Column="1">
      <Label Content="A WPF label" FontSize="24"/>
      <WindowsFormsHost Canvas.Top="20" Canvas.Left="20" Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 按 F5 生成并运行该应用程序。 WindowsFormsHost 元素绘制在标签元素的上面。

停靠

WindowsFormsHost 元素支持 WPF 停靠。 设置 Dock 附加属性,以便将所承载的控件停靠在 DockPanel 元素中。

停靠所承载的控件

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Docking a WindowsFormsHost element. -->
    <DockPanel LastChildFill="false"  Grid.Row="2" Grid.Column="1">
      <WindowsFormsHost DockPanel.Dock="Right"  Canvas.Top="20" Canvas.Left="20" Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </DockPanel>
    
  2. 按 F5 生成并运行该应用程序。 WindowsFormsHost 元素停靠在 DockPanel 元素的右侧。

设置可见性

通过在 WindowsFormsHost 元素上设置 Visibility 属性,可以使 Windows Forms控件不可见或将它折叠。 当控件不可见时,它不会显示,但会占据布局空间。 当控件折叠时,它不会显示,也不会占据布局控件。

设置所承载控件的可见性

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Setting Visibility to hidden and collapsed. -->
    <StackPanel Grid.Row="3" Grid.Column="1">
      <Button Name="button2" Click="button2_Click" Content="Click to make invisible" Background="OrangeRed"/>
      <WindowsFormsHost Name="host1"  Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
      <Button Name="button3" Click="button3_Click" Content="Click to collapse" Background="OrangeRed"/>
    </StackPanel>
    
  2. 在 MainWindow.xaml.vb 或 MainWindow.xaml.cs 中,将以下代码复制到类定义中。

    Private Sub button2_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.host1.Visibility = Windows.Visibility.Hidden
    End Sub
    
    
    Private Sub button3_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
        Me.host1.Visibility = Windows.Visibility.Collapsed
    End Sub
    
    private void button2_Click(object sender, EventArgs e)
    {
        this.host1.Visibility = Visibility.Hidden;
    }
    
    private void button3_Click(object sender, EventArgs e)
    {
        this.host1.Visibility = Visibility.Collapsed;
    }
    
  3. 按 F5 生成并运行该应用程序。

  4. 单击**“Click to make invisible”(单击隐藏)**按钮使 WindowsFormsHost 元素不可见。

  5. 单击**“Click to collapse”(单击折叠)**按钮使 WindowsFormsHost 元素完全从布局中隐藏。 当 Windows Forms控件折叠时,周围的元素会重新排列以占据它的空间。

承载不拉伸的控件

一些 Windows Forms控件具有固定的大小,不会拉伸以填满布局中的可用空间。 例如,MonthCalendar 控件在固定的空间中显示月份。

承载不拉伸的控件

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Hosting a control that does not stretch. -->
    <!-- The MonthCalendar has a discrete size. -->
    <StackPanel Grid.Row="4" Grid.Column="1">
      <Label Content="A WPF element" Background="OrangeRed"/>
      <WindowsFormsHost Background="Yellow">
        <wf:MonthCalendar/>
      </WindowsFormsHost>
      <Label Content="Another WPF element" Background="OrangeRed"/>
    </StackPanel>
    
  2. 按 F5 生成并运行该应用程序。 WindowsFormsHost 元素在网格行中居中,但它不会拉伸以填满可用空间。 如果窗口足够大,您可能会看到所承载的 MonthCalendar 控件显示两个或更多个月份,但这些月份在一行中居中。 WPF 布局引擎使不能调整大小以填满可用空间的元素居中。

缩放

与 WPF 元素不同,大多数 Windows Forms控件都不是可连续缩放的。 如果可能,WindowsFormsHost 元素会对它所承载的控件进行缩放。

对所承载的控件进行缩放

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Scaling transformation. -->
    <StackPanel Grid.Row="0" Grid.Column="2">
    
      <StackPanel.RenderTransform>
        <ScaleTransform CenterX="0" CenterY="0" ScaleX="0.5" ScaleY="0.5" />
      </StackPanel.RenderTransform>
    
      <Label Content="A WPF UIElement" Background="OrangeRed"/>
    
      <WindowsFormsHost Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    
      <Label Content="Another WPF UIElement" Background="OrangeRed"/>
    
    </StackPanel>
    
  2. 按 F5 生成并运行该应用程序。 所承载的控件及其周围元素按 0.5 的比例进行缩放。 但是,所承载控件的字体不缩放。

旋转

与 WPF 元素不同,Windows Forms控件不支持旋转。 当应用旋转变换时,WindowsFormsHost 元素不随其他 WPF 元素一起旋转。 180 度以外的任何旋转值都会引发 LayoutError 事件。

查看混合应用程序中的旋转效果

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Rotation transformation. -->
    <StackPanel Grid.Row="1" Grid.Column="2">
    
      <StackPanel.RenderTransform>
        <RotateTransform CenterX="200" CenterY="50" Angle="180" />
      </StackPanel.RenderTransform>
    
      <Label Content="A WPF element" Background="OrangeRed"/>
    
      <WindowsFormsHost Background="Yellow">
        <wf:Button Text="Windows Forms control" FlatStyle="Flat"/>
      </WindowsFormsHost>
    
      <Label Content="Another WPF element" Background="OrangeRed"/>
    
    </StackPanel>
    
  2. 按 F5 生成并运行该应用程序。 所承载的控件不旋转,但是它周围的元素旋转 180 度。 您可能必须调整窗口大小才能看到这些元素。

设置空白和边距

WPF 布局中的空白和边距与 Windows Forms中的空白和边距类似。 只需在 WindowsFormsHost 元素上设置 PaddingMargin 属性。

为承载的控件设置空白和边距

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Padding. -->
    <Canvas Grid.Row="2" Grid.Column="2">
      <WindowsFormsHost Padding="0, 20, 0, 0" Background="Yellow">
        <wf:Button Text="Windows Forms control with padding" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
    
    ...
    
    
    <!-- Margin. -->
    <Canvas Grid.Row="3" Grid.Column="2">
      <WindowsFormsHost Margin="20, 20, 0, 0" Background="Yellow">
        <wf:Button Text="Windows Forms control with margin" FlatStyle="Flat"/>
      </WindowsFormsHost>
    </Canvas>
    
  2. 按 F5 生成并运行该应用程序。 空白和边距设置应用于承载的 Windows Forms控件,就像在 Windows Forms中应用一样。

使用动态布局容器

Windows Forms提供两个动态布局容器,即 FlowLayoutPanelTableLayoutPanel。 您还可以在 WPF 布局中使用这些容器。

使用动态布局容器

  1. 将下面的 XAML 复制到 Grid 元素中。

    <!-- Flow layout. -->
    <DockPanel Grid.Row="4" Grid.Column="2">
      <WindowsFormsHost Name="flowLayoutHost" Background="Yellow">
        <wf:FlowLayoutPanel/>
      </WindowsFormsHost>
    </DockPanel>
    
  2. 在 MainWindow.xaml.vb 或 MainWindow.xaml.cs 中,将以下代码复制到类定义中。

    Private Sub InitializeFlowLayoutPanel()
        Dim flp As System.Windows.Forms.FlowLayoutPanel = Me.flowLayoutHost.Child
    
        flp.WrapContents = True
    
        Const numButtons As Integer = 6
    
        Dim i As Integer
        For i = 0 To numButtons
            Dim b As New System.Windows.Forms.Button()
            b.Text = "Button"
            b.BackColor = System.Drawing.Color.AliceBlue
            b.FlatStyle = System.Windows.Forms.FlatStyle.Flat
    
            flp.Controls.Add(b)
        Next i
    
    End Sub
    
    private void InitializeFlowLayoutPanel()
    {
        System.Windows.Forms.FlowLayoutPanel flp =
            this.flowLayoutHost.Child as System.Windows.Forms.FlowLayoutPanel;
    
        flp.WrapContents = true;
    
        const int numButtons = 6;
    
        for (int i = 0; i < numButtons; i++)
        {
            System.Windows.Forms.Button b = new System.Windows.Forms.Button();
            b.Text = "Button";
            b.BackColor = System.Drawing.Color.AliceBlue;
            b.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
    
            flp.Controls.Add(b);
        }
    }
    
  3. 在构造函数中添加对 InitializeFlowLayoutPanel 方法的调用。

    Public Sub New()
        InitializeComponent()
    
        Me.InitializeFlowLayoutPanel()
    
    End Sub
    
    public MainWindow()
    {
        InitializeComponent();
    
        this.InitializeFlowLayoutPanel();
    }
    
  4. 按 F5 生成并运行该应用程序。 WindowsFormsHost 元素填充 DockPanelFlowLayoutPanel 按默认 FlowDirection 排列它的子控件。

请参见

参考

ElementHost

WindowsFormsHost

概念

WindowsFormsHost 元素的布局注意事项

演练:在 WPF 中承载 Windows 窗体复合控件

演练:在 Windows 窗体中承载 WPF 复合控件

其他资源

WPF 设计器

Arranging Windows Forms Controls in WPF Sample

修订记录

日期

修订记录

原因

2010 年 8 月

针对 Visual Studio 2010 进行了更新。

客户反馈