WPF教程

WPF和WINFORM的互操作--WPF教程

时间:2013-7-23 22:22:16  作者:WPF之家  来源:http://www.wpf123.com  查看:367  评论:0
内容摘要: 在WPF中使用Winform控件<Window x:Class="WPFApplication.Window1"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presenta...

 

WPF中使用Winform控件

<Window x:Class="WPFApplication.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:winforms="clr-namespace:WindowsFormsControl;assembly=WindowsFormsControl"
xmlns:my="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
Title="Window1" Height="300" Width="300" >
<Grid>
<my:WindowsFormsHost Name="windowsFormsHost1">
<winforms:UserControl1 x:Name="myControl" ButtonText="Click me!" />
</my:WindowsFormsHost>
</Grid>
</Window>
其中xmlns:winforms=…为导入命名空间和程序集名称
<Grid>中的标记为winform用户控件
 

Winform中使用WPF控件,需要添加以下几个与WPF相关的引用:
* PresentationCore
* PresentationFramework
* System.Xaml
* WindowsBase
* WindowsFormsIntegration

 

void WPFInWinform()
{
//创建WPF控件
System.Windows.Controls.TextBox wpfTxt = new System.Windows.Controls.TextBox();
wpfTxt.Name = "txName";
wpfTxt.Text = "WPF TextBox";
//创建使用WPF控件的容器
ElementHost elementHost = new ElementHost();
elementHost.Dock = DockStyle.None;
elementHost.Width = 150;
elementHost.Height = 50;

elementHost.Child = wpfTxt;
this.Controls.Add(elementHost);
}


标签:WPF和WINFORM的互操作 

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。本文出自“wpf之家”,请务必保留此出处:http://www.wpf123.com

相关评论
Copyright © 2009-2014 WPF之家(http://www.wpf123.com/) All rights reserved
 Powered by WPF之家
鄂ICP备13006396号