Using windows forms controls on a WPF page

2019-03-04 06:55发布

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 ?

1条回答
Fickle 薄情
2楼-- · 2019-03-04 07:16

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

   this.Children.Add(HOST);
查看更多
登录 后发表回答