是否可以在窗体内显示xaml视图

时间:2012-04-25 16:56:29

标签: c# wpf

我有一个Windows窗体,可以调用在我的应用程序中工作的其他窗体。我想要完成的是远离整个“windows窗体”的东西,而是使用WPF View(usercontrol)。有没有办法可以调用视图从我的表单中显示它?

ElementHost host = new ElementHost();
            Cars.WPF.Views.DescriptionView descView = new Cars.WPF.Views.DescriptionView();
            host.Controls.Add(descView);
            host.Dock = DockStyle.Fill;

我收到错误: - >参数1:无法从'Car.WPF.Views.DescriptionView'转换为'System.Windows.Forms.Control'

2 个答案:

答案 0 :(得分:2)

在winform中添加一个面板(比如panel1) 在类级别定义ElementHost,还在类级别定义WPF控件

ElementHost host;
Cars.WPF.Views.DescriptionView descView;

在表单加载事件中执行:

host= new ElementHost();
panel1.Controls.Add(ctrlHost); //Add Element host to panel1
descView = new Cars.WPF.Views.DescriptionView();
descView.InitializeComponent();
host.Child = descView; //Instead of adding WPF control to Winform do this

同样在您的项目参考中添加:

PresentationCore
PresentationFramework
WindowsBase

答案 1 :(得分:1)