2012-04-25 45 views
1

我有一个窗体窗体,它调用其他窗体窗体,在我的应用程序中工作。我想完成的是摆脱这整个“窗体”的东西,并使用WPF视图(usercontrol)来代替。有没有一种方法可以将视图从视图中显示出来?是否可以在Windows窗体中显示xaml视图

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

在你的winform添加面板(可以说PANEL1) 在类级别定义ElementHost的,此外,在一流水平

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

定义WPF控件在窗体加载事件做:

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
+0

我试过使用元素主机,但它不喜欢它。 – Calvin 2012-04-25 16:59:09

+0

它不喜欢什么。 WPF UserControl不显示?绑定是否有效?键盘输入无法识别?你有没有试过简单的开始,只需用一个简单的WPF控件启动一个新窗体?让它工作? – 2012-04-25 17:01:22

+0

我已更新我的示例。 – Calvin 2012-04-25 17:01:29

相关问题