在WPF中使用WinForm控件方法

1、      首先添加对如下两个dll文件的引用:WindowsFormsIntegration.dll,System.Windows.Forms.dll。

2、      在要使用WinForm控件的WPF窗体的XAML文件中添加如下内容:

即:

 xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

3、       在WPF的容器控件内如StackPanel内首先要添加WinForm控件的宿主容器,用于衔接WPF和WinForm,

对应XAML如下:

 <StackPanel>
            <wfi:WindowsFormsHost>
                <wf:Label x:Name="wfLabel" Text="winForm控件在此" />
            </wfi:WindowsFormsHost>

            <wfi:WindowsFormsHost>
                <wf:Button x:Name="wfButton" Text="确定" Click="wfButton_Click" />
            </wfi:WindowsFormsHost>           

            <Button Content="Button" Margin="10" Name="button1"/>
</StackPanel>

说明:<wfi:WindowsFormsHost></wfi:WindowsFormsHost>即为WinForm控件的宿主容器,每一个宿主容器只能放一个WinForm控件,如下例,放了三个WinForm控件,分别放在三个宿主容器里面,该容器可以设置属性来调整大小和布局。

注意:如上我添加的WinForm控件如在指定其Name时,必须加前缀x:,如添加Lable时<wf:Label x:Name="wpfLabel" Text="我是WPF中的WinForm控件” />,否则后台代码无法访问。

4、       如果要在WPF后台代码中访问上面的Lable,可直接像在WinForm中使用一样。如在点击某一按钮时改变Lable内容,代码如下:wpfLabel.Text=”内容已改变”;

5、       以使用WinForm控件wpfLabel改变标记为例,说明后台用法:

完整后台代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace ChangeDetection
{
    /// <summary>
    /// Window4.xaml 的交互逻辑
    /// </summary>
    public partial class Window4 : Window
    {
        public Window4()
        {
            InitializeComponent();
        }
        
        private void wfButton_Click(object sender, EventArgs e)
        {
            wfLabel.Text= "内容已改变";

        }
    }
}

完整XAML如下:

<Window x:Class="ChangeDetection.Window4"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ChangeDetection"
        xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
        xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
        mc:Ignorable="d"
        Title="Window4" Height="300" Width="300">
    
        <StackPanel>
            <wfi:WindowsFormsHost>
                <wf:Label x:Name="wfLabel" Text="winForm控件在此" />
            </wfi:WindowsFormsHost>

            <wfi:WindowsFormsHost>
                <wf:Button x:Name="wfButton" Text="确定" Click="wfButton_Click" />
            </wfi:WindowsFormsHost>           

            <Button Content="Button" Margin="10" Name="button1"/>
        </StackPanel>

</Window>

 

posted @ 2016-10-21 10:04  JeffreyGoogle  阅读(17992)  评论(2编辑  收藏  举报