Netrix Component has full support for styles. You can:
This document can be modified by adding an external stylesheet. This is done by adding a LINK element. The style sheet file is copied to the assemblies exeutable folder.
Assume you have a dialog where a style editor is present, the following code writes and shows the current element's styles:
IElement currentElement = EditorDocument.HtmlEditor.GetCurrentElement(); string cssText = currentElement.GetStyle(); if (editStylesDialog.ShowDialog(this) == System.Windows.Forms.DialogResult.OK) { currentElement.SetStyle(cssText); }
Style sheets are linked through a LINK element in the HEAD section. The HtmlDocumentStructure class exposes this (and other parts) as collections. The code shown next adds and removes a stylesheet accordingly to the state of the ribbon's toggle button.
void _toggleButtonStylesLinkedStylesEnabled_ExecuteEvent(object sender, ExecuteEventArgs e) { bool enable = _toggleButtonStylesLinkedStylesEnabled.BooleanValue; // assume the file "LinkedStylesheet.css" is in the output directory // for the first time, we add it to the current document bool alreadyLinked = false; if (EditorDocument.HtmlEditor.DocumentStructure.LinkedStylesheets.Count > 0) { IElement linkedCss = EditorDocument.HtmlEditor.GetElementById("LinkElement2134"); if (linkedCss != null && linkedCss is LinkElement) { alreadyLinked = true; linkedCss.ElementDom.RemoveElement(false); } } string css = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "Resources\\Css\\LinkedStylesheet.css"); if (File.Exists(css) && !alreadyLinked && enable) { LinkElement link = EditorDocument.HtmlEditor.CreateElement("LINK") as LinkElement; link.rel = "Stylesheet"; link.media = LinkElementMedia.All; link.type = "text/css"; link.href = css; link.ID = "LinkElement2134"; EditorDocument.HtmlEditor.DocumentStructure.LinkedStylesheets.Add(link); } if (enable) { MessageBox.Show(String.Format("{0} linked styles found", EditorDocument.HtmlEditor.DocumentStructure.LinkedStylesheets.Count), "Linked Stylesheet", MessageBoxButtons.OK, MessageBoxIcon.Information); } }
There are several techniques involved here. The LINK element is created using the CreateElement method. The required attributes are set properly. Then the element is being added to the collection of stylesheet. By default this collection is empty. You can add as many stylesheets as you like.
If the link is about to be removed the element is first retrieved using its ID. If it is there, the ElementDom class is used to remove it from the document.
This is, however, a complete access to the documents content. If you like to disable the style temporarily, just to check an effect first, you can use this call:
EditorDocument.HtmlEditor.LinkedStylesheetsEnabled = enable;
The stylesheet has no effect if disabled, but the LINK element is still in the document.
The style parser parses a stylesheet and creates a dictionary of styles, each as an object. The style dialog uses this code:
public string CssText { get { return styleControl1.StyleString; } set { styleControl1.StyleString = value; } } public IElement Element { set { if (value != null) { labelElement.Text = value.TagName.ToUpper(); styleControl1.Enabled = true; textBoxStyles.Enabled = false; } else { labelElement.Text = ""; styleControl1.Enabled = false; textBoxStyles.Enabled = false; } } } private void styleControl1_ParserReady(object sender, SelectorEventArgs e) { textBoxStyles.Text = e.Selector.ToString(); } private void styleControl1_ContentChanged(object sender, EventArgs e) { textBoxStyles.Text = styleControl1.StyleString; }
Two events are used. ParserReady is fired after the stylesheet has been parsed. Here it is used to sync the textbox. When the user changes a style in the control, the parser fires the ContentChanged event. The current style is always available through the style control's StyleString property.