惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

U
Unit 42
小众软件
小众软件
Y
Y Combinator Blog
S
SegmentFault 最新的问题
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
人人都是产品经理
人人都是产品经理
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
L
LangChain Blog
Martin Fowler
Martin Fowler
美团技术团队
B
Blog RSS Feed
GbyAI
GbyAI
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
大猫的无限游戏
大猫的无限游戏
博客园 - 司徒正美
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Hugging Face - Blog
Hugging Face - Blog
D
Docker
G
Google Developers Blog

博客园 - Sheva

Two Xaml Tricks - Sheva I See Cool Stuff Today DataTemplate Is A Bit Naughtier To Play With XmlnsDefinitionAttribute Is Pretty Nifty Application.DoEvents In WPF 征召译者 C# Trivia Test CreateParams Is Pretty Cool! In-Place Editing In WPF SingleLinkedList Implementation Data Binding In WPF C# Application Markup Language Regex 101 Exercise I10 - Extract repeating hex blocks from a string AvalonPaint: Features Added Regex 101 Exercise I9 - Count the number of matches Crossbow: Hosting WPF Controls In Winforms Application The Architecture Of Avalon Drop Shadow Effect In WPF XamlReader.Load(): Build Up Your Own XamlPad
Crossbow? WTF?
Sheva · 2006-02-28 · via 博客园 - Sheva

Posted on 2006-02-28 01:37  Sheva  阅读(5406)  评论()    收藏  举报

    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