Crossbow? WTF?

Source: Internet
Author: User
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 shoshould 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 shoshould 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 (. 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 shocould 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 shoshould do for hosting Windows Forms control in WPF, Let's see our little application in action:




For complete source code, please check here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.