在WPF页面上使用窗体控件 [英] Using windows forms controls on a WPF page

查看:116
本文介绍了在WPF页面上使用窗体控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个WPF页面上的datagridview。



我使用这个

 code> public WindowsFormsHost HOST = new WindowsFormsHost(); 

然后将我的datagridview简化为

  System.Windows.Forms.DataGridView gridview = new System.Windows.Forms.DataGridView(); 

然后使用datatable我将datagrid视图填充为



//使WPF应用程序已知的本地控件

  HOST.Child = gridview; 
//显示列标题(从datatable)。
gridview.DataSource = table.DefaultView;

但是,当我将gridview添加到我的WPF窗口中

  this.Children.Add(gridview); //这行上的错误

我收到错误说

 无法从System.Windows.Forms.DataGridView转换为System.Windows.UIElement 

为什么这样?

解决方案

你应该在WPF窗口中添加HOST比添加'gridview'。

  this.Children.Add(HOST); 


Guys I need a datagridview on a WPF page.

I used this

public WindowsFormsHost HOST = new WindowsFormsHost();

Then instanttiated my datagridview as

System.Windows.Forms.DataGridView gridview = new System.Windows.Forms.DataGridView(); 

Then using a datatable I filled the datagrid view as

//making the native control known to the WPF application

HOST.Child = gridview;
    //Displaying the column headers  (from datatable). 
gridview.DataSource = table.DefaultView;

However when I add the gridview to my WPF window as

this.Children.Add(gridview);   //error at this line

I get an error saying

cannot convert from 'System.Windows.Forms.DataGridView' to 'System.Windows.UIElement

Why so ?

解决方案

You should add 'HOST' to WPF Window rather than adding 'gridview'.

   this.Children.Add(HOST);

这篇关于在WPF页面上使用窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆