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);
}
{
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;
}
{
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