如何在WPF中调用Winform控件
2009-03-31 10:09
218 查看
Crossbow, OMG, WTF?
Well, Crossbow is the codename for the interoperability technology between Windows Forms and Windows Presentation Foundation. Crossbow enables you to host Windows Forms control in your WPF application, and vice versa. In this post, I will demonstrate how to use Windows Forms control in WPF application, in my next post, I will demonstrate another simple ridiculous Windows Forms application which will host Windows Presentation Foundation controls.
So, the first step is to add some assembly reference into your WPF application project. there are two important assemblies you should include, they are WindowsFormsIntegration.dll and System.Windows.Forms.dll, after you're done with this, you can reference Windows.Forms.Integration and System.Windows.Forms namespaces in the Xaml code as follows:
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hosting Windows Forms Control In WPF"
Height="300"
Width="650"
ResizeMode="NoResize"
Loaded="WindowLoadedHandler"
>
<DockPanel>
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml -->
<wf:DataGridView x:Name="dataGridView" Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
</DockPanel>
</Window>
Note that I declare two additional Xml namespace references in Xaml so that the Xaml parser knows how to parse the Windows Forms control tag.
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Then you should declare the WindowsFormsHost control in the Xaml, this control acts like a sticky point between WPF and Windows Forms.
After that, you can declare any Windows Forms control within the WindowsFormsHost control, here we add a DataGridView control for displaying the profiles of some famous soccer players.
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml -->
<wf:DataGridView x:Name="dataGridView" Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
Note that you can declaratively set the properties of Windows Forms control(a.k.a DataGridView) in Xaml. and of course you can also set this control's properties in code, here we set additional DataGridView's propeties in code, and then bind the profile data with this control, all those plumbing code should be in the Window's Loaded event handler method:
private void WindowLoadedHandler(Object sender, EventArgs e)
{
// Set some properties on Windows Forms control in code.
dataGridView.AlternatingRowsDefaultCellStyle.BackColor = GdiPlus::SystemColors.InactiveCaptionText;
dataGridView.AllowUserToAddRows = true;
// Load players information.
DataSet ds = new DataSet();
ds.ReadXml("..\\..\\players.xml");
playerBindingSource = new Winforms::BindingSource(ds, "player");
dataGridView.DataSource = playerBindingSource;
}
Note that you cannot move the above code into the Window's constructor, if you do so, an exception will be thrown.
Okay, this is all what we should do for hosting Windows Forms control in WPF, let's see our little application in action:
For complete source code, please check here
Well, Crossbow is the codename for the interoperability technology between Windows Forms and Windows Presentation Foundation. Crossbow enables you to host Windows Forms control in your WPF application, and vice versa. In this post, I will demonstrate how to use Windows Forms control in WPF application, in my next post, I will demonstrate another simple ridiculous Windows Forms application which will host Windows Presentation Foundation controls.
So, the first step is to add some assembly reference into your WPF application project. there are two important assemblies you should include, they are WindowsFormsIntegration.dll and System.Windows.Forms.dll, after you're done with this, you can reference Windows.Forms.Integration and System.Windows.Forms namespaces in the Xaml code as follows:
<Window x:Class="CrossBowDemo.MainWindow"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Hosting Windows Forms Control In WPF"
Height="300"
Width="650"
ResizeMode="NoResize"
Loaded="WindowLoadedHandler"
>
<DockPanel>
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml -->
<wf:DataGridView x:Name="dataGridView" Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
</DockPanel>
</Window>
Note that I declare two additional Xml namespace references in Xaml so that the Xaml parser knows how to parse the Windows Forms control tag.
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Then you should declare the WindowsFormsHost control in the Xaml, this control acts like a sticky point between WPF and Windows Forms.
After that, you can declare any Windows Forms control within the WindowsFormsHost control, here we add a DataGridView control for displaying the profiles of some famous soccer players.
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml -->
<wf:DataGridView x:Name="dataGridView" Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
Note that you can declaratively set the properties of Windows Forms control(a.k.a DataGridView) in Xaml. and of course you can also set this control's properties in code, here we set additional DataGridView's propeties in code, and then bind the profile data with this control, all those plumbing code should be in the Window's Loaded event handler method:
private void WindowLoadedHandler(Object sender, EventArgs e)
{
// Set some properties on Windows Forms control in code.
dataGridView.AlternatingRowsDefaultCellStyle.BackColor = GdiPlus::SystemColors.InactiveCaptionText;
dataGridView.AllowUserToAddRows = true;
// Load players information.
DataSet ds = new DataSet();
ds.ReadXml("..\\..\\players.xml");
playerBindingSource = new Winforms::BindingSource(ds, "player");
dataGridView.DataSource = playerBindingSource;
}
Note that you cannot move the above code into the Window's constructor, if you do so, an exception will be thrown.
Okay, this is all what we should do for hosting Windows Forms control in WPF, let's see our little application in action:
For complete source code, please check here
相关文章推荐
- 转:如何在多线程中调用winform窗体控件
- 如何在多线程中调用winform窗体控件(转)
- WPF闪烁预警动画的控件制作及winform调用WPF控件的方法
- 如何在多线程中调用winform窗体控件
- WinForm中如何使用WPF中的控件
- C# 如何在winform中使用wpf用户控件
- 如何在多线程中调用winform窗体控件
- 如何在多线程中调用winform窗体控件
- 如何在多线程中调用winform窗体控件
- WinForm异步:如何:对 Windows 窗体控件进行线程安全调用
- WPF调用winform(基于GDI)的控件和WPF窗体无边框的处理
- 如何在多线程中调用winform窗体控件
- 如何在多线程中调用winform窗体控件
- WPF调用Winform控件具体技巧
- C# winform中一个类中如何调用另一个窗体的控件或方法
- 如何在多线程中调用winform窗体控件
- 如何在多线程中调用winform窗体控件
- 如何在多线程中调用winform窗体控件
- Jquery如何序列化form表单数据为JSON对象 C# ADO.NET中设置Like模糊查询的参数 从客户端出现小于等于公式符号引发检测到有潜在危险的Request.Form 值 jquery调用iframe里面的方法 Js根据Ip地址自动判断是哪个城市 【我们一起写框架】MVVM的WPF框架(三)—数据控件 设计模式之简单工厂模式(C#语言描述)
- 如何在多线程中调用winform窗体控件2――实例篇