






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>
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
<wfi:WindowsFormsHost>
<!-- Set some properties on Windows Forms control in Xaml -->
<wf:DataGridView x:Name="dataGridView" Dock="Fill" SelectionMode="FullRowSelect"/>
</wfi:WindowsFormsHost>
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;
}
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。