Volunteer .NET Evangelist

A well oiled machine can’t run efficiently, if you grease it with water.
posts - 44, comments - 88, trackbacks - 2, articles - 0
  首页  :: 联系 :: 订阅 订阅  :: 管理

Crossbow: Hosting WPF Controls In Winforms Application

Posted on 2006-02-28 23:26  Sheva  阅读(...)  评论(...编辑  收藏
    For all of you who follow up my blog closely, you will probably remember that I have written a little application in which you can edit your xaml snippet and have that sinppet executed in real time, and this is what exactly the XamlPad.exe which is shipped with the WINFX SDK does. But in that little application, I just used the plain text editor for editing Xaml, you know, no syntax highlighting, no line numbering, no auto indentation etc. Fortunately the TextEditorControl which is available from the SharpDevelop project meets exactly what I need. So I can reuse that control without reinventing the wheel. I have tried to use the TextEditorControl in my WPF version of XamlPad, but It fails, even fails with no clue. But I know this control can work pretty well in Winforms application, so instead of hosting this control in WPF, I just use this control in Winforms application, and use Crossbow to host the necessary WPF controls in this Winforms application.
    Just as before, you have to include some important assemblies into your project, they are WindowsBase.dll, PresentationCore.dll, PresentationFramework.dll, UIAutomationProvider.dll, UIAutomationTypes.dll, and WindowsFormsIntegration.dll.
    Then you can add a Winforms TabControl into containing Form through classical Drag & Drop operation, and set those properties for the TabControl instance, immediately after that you can write something like the following in code:
 private void SetupExecutionPad()
        {
            ElementHost host 
= new ElementHost();
            host.Dock 
= DockStyle.Fill;
            executionPad 
= new ContentControl();
            host.Controls.Add(executionPad);
            tabExectionView.Controls.Add(host);
        }

    This private helper method will perform the necessary operations to host WPF control. First, we need to instantiate an ElementHost control, Just as WindowsFormsHost control does in WPF, this control is something like a wrapper or container which can has a single WPF control as its child control. and then you can create any WPF control(here we create a ContentControl for displaying the dynamically compiled Xaml) and add it to the ElementHost control's ControlCollection.
    Next, we should initialize some TextEditorControl's properties to make it support XML syntax highlighting and other flashy features.
private void SetupXamlEditor()
        {
            xamlEditor.Document.HighlightingStrategy 
= HighlightingStrategyFactory.CreateHighlightingStrategy("XML");
            xamlEditor.Document.FormattingStrategy 
= new DefaultFormattingStrategy();
            DefaultTextEditorProperties properties 
= new DefaultTextEditorProperties();
            properties.Font 
= new Font("Verdana", 10f);
            properties.IndentStyle 
= IndentStyle.Smart;
            properties.ShowSpaces 
= false;
            properties.LineTerminator 
= "\r\n";
            properties.ShowTabs 
= false;
            properties.ShowInvalidLines 
= false;
            properties.ShowEOLMarker 
= false;
            properties.UseAntiAliasedFont 
= true;
            properties.EnableFolding 
= true;
            properties.TabIndent 
= 3;
            properties.AllowCaretBeyondEOL 
= false;
            properties.AutoInsertCurlyBracket 
= true;
            properties.BracketMatchingStyle 
= BracketMatchingStyle.After;
            properties.ConvertTabsToSpaces 
= false;
            properties.ShowMatchingBracket 
= true;
            xamlEditor.TextEditorProperties 
= properties;
        }

    Okay, this is what we should do to revamp the XamlPad application, the following is the screenshot of this little fancy application:

    XamlPad in ExecutionView:



    XamlPad in XamlView


    To make this application a full-blown Xaml IDE, there are hoards of things which I have to do, but I just need a simple primitive XamlPad, and this one serves me pretty well in writing Xaml snippets.
    For all of you who are interested in this little application, you can download its source code from here