WPF控件的铸造
本文关键字:控件 WPF | 更新日期: 2024-09-19 23:12:39
我正在使用WindowsFormHost以便能够在我的WPF应用程序中使用网格视图(从这个开始,如何实例化Datagridview(在代码后面))。
并作为进行
public WindowsFormsHost HOST = new WindowsFormsHost();
我的网格视图为
System.Windows.Forms.DataGridView gridview = new System.Windows.Forms.DataGridView();
(不要忘记参考资料,请点击此处-http://www.c-sharpcorner.com/uploadfile/mahesh/using-windows-forms-controls-in-wpf/)
我用的数据表填充它
table = new System.Data.DataTable();
table.Columns.Add("Object ID");
foreach (string column in columns)
{
table.Columns.Add(column);
}
foreach (string row in rows)
{
drow = table.NewRow();
tit = row.Substring(0, row.IndexOf('$'));
drow[0] = tit.IndexOf('&') > -1 ? tit.Substring(tit.IndexOf('&') + 1) : tit;
table.Rows.Add(drow);
}
//making the native control known to the WPF application
HOST.Child = gridview;
//Displaying the column headers of the listbox(assigned above).
gridview.DataSource = table.DefaultView;
然而,当我将网格视图作为添加到我的WPF窗口时
this.Children.Add(gridview); //error at this line
我在说时出错
cannot convert from 'System.Windows.Forms.DataGridView' to 'System.Windows.UIElement'
为什么会这样?我的意思是,我可以做些什么来纠正这一点?
DataGridView
是一个WinForms
控件。所以您不能直接将其添加到WPF控件的子项中。相反,添加WindowsFormsHost
实例如下:
RootGrid.Children.Add(HOST);
这个问题和当前答案是认真的吗???您究竟为什么要导入WindowsForms dll并将CPU密集型WindowsFormsHost
元素添加到WPF应用程序中,以便使用DataGridView
控件?您会发现,只使用等效的WPF控件:DataGrid
要好得多
通过查看MSDN上的DataGrid
类页面,您可以了解有关DataGrid
类的所有信息。此页面有详细的描述和代码示例,可帮助您使用控件。此外,CodeProject上的WPF DataGrid Practical Examples页面提供了关于如何使用DataGrid
控件执行任何操作的详细示例。
伙计们,我正在为未来的访客发布对我有用的东西。我没有添加网格视图,而是将HOST添加到窗口中,结果就成功了。
this.Children.Add(HOST);
在这里解决:在WPF页面上使用windows窗体控件