Ribbon and Command Bars Example for WPF

Nevron NOV RichTextEditor for WPF ships with hundreds of examples with full source code, as part of the Nevron Open Vision Suite. All WPF RichTextEditor examples are also available as a stand-alone Visual Studio solution in C# and VB.NET, which you can download from the links below.

Ribbon and Command Bars Example for WPF Description

This example demonstrates how to add commanding interface to the NOV Rich Text Editor and how to switch between ribbon and command bars.


Ribbon and Command Bars Example

C# Ribbon and Command Bars Example Code for Nevron RichTextEditor for WPF
Language: C#

using Nevron.Nov;
using Nevron.Nov.Dom;
using Nevron.Nov.Graphics;
using Nevron.Nov.Layout;
using Nevron.Nov.Text;
using Nevron.Nov.Text.UI;
using Nevron.Nov.UI;
using Nevron.Nov.Windows.Forms;
using System.Windows.Forms;

namespace NovWinFormsProject
{
	public partial class Form1 : Form
	{
		#region Constructors

		public Form1()
		{
			InitializeComponent();

			// Set form title
			Text = "NOV WinForms - NRibbonAndCommandBarsExample";

			// Create the NOV example and its controls
			NWidget exampleContent = CreateExampleContent();
			NWidget exampleControls = CreateExampleControls();
			if (exampleControls != null)
			{
				exampleControls.PreferredWidth = 300;
			}

			// Place the example and its controls in a pair box
			NPairBox pairBox = new NPairBox(exampleContent, exampleControls);
			pairBox.FillMode = ENStackFillMode.First;
			pairBox.FitMode = ENStackFitMode.First;
			pairBox.Margins = new NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing);

			// Host the example in the form
			NNovWidgetHost host = new NNovWidgetHost(pairBox);
			host.Dock = DockStyle.Fill;
			Controls.Add(host);
		}

		#endregion

		#region Example

		public NWidget CreateExampleContent()
		{
			m_RichText = new NRichTextView();
			m_RichText.AcceptsTab = true;
			m_RichText.Content.Sections.Clear();

			// Populate the rich text
			PopulateRichText();

			// Create and execute a ribbon UI builder
			m_RibbonBuilder = new NRichTextRibbonBuilder();
			return m_RibbonBuilder.CreateUI(m_RichText);
		}
		public NWidget CreateExampleControls()
		{
			// Switch UI button
			NButton switchUIButton = new NButton(SwitchToCommandBars);
			switchUIButton.VerticalPlacement = ENVerticalPlacement.Top;
			switchUIButton.Click += OnSwitchUIButtonClick;

			return switchUIButton;
		}
		public string GetExampleDescription()
		{
			return "

This example demonstrates how to add commanding interface to the NOV Rich Text Editor and how to switch between ribbon and command bars.

"; } /// /// Gets the relevant documentation links for the example /// /// public (string Name, string Url)[] GetDocumentationUrls() { return new[] { ("Rich Text Ribbon Overview", "https://www.nevron.com/docs/openvision/rich-text-ribbon-overview.html"), ("Rich Text Command Bars Overview", "https://www.nevron.com/docs/openvision/rich-text-command-bars-overview.html"), }; } private void PopulateRichText() { NSection section = new NSection(); m_RichText.Content.Sections.Add(section); section.Blocks.Add(GetDescriptionBlock("Rich Text Ribbon and Command Bars", "This example demonstrates how to customize the NOV rich text ribbon.", 1)); } #endregion #region Implementation private void SetUI(NCommandUIHolder oldUiHolder, NWidget widget) { if (oldUiHolder.ParentNode is NTabPage) { ((NTabPage)oldUiHolder.ParentNode).Content = widget; } else if (oldUiHolder.ParentNode is NPairBox) { ((NPairBox)oldUiHolder.ParentNode).Box1 = widget; } } #endregion #region Event Handlers private void OnSwitchUIButtonClick(NEventArgs arg) { NButton switchUIButton = (NButton)arg.TargetNode; NLabel label = (NLabel)switchUIButton.Content; // Remove the rich text view from its parent NCommandUIHolder uiHolder = m_RichText.GetFirstAncestor(); m_RichText.ParentNode.RemoveChild(m_RichText); if (label.Text == SwitchToRibbon) { // We are in "Command Bars" mode, so switch to "Ribbon" label.Text = SwitchToCommandBars; // Create the ribbon SetUI(uiHolder, m_RibbonBuilder.CreateUI(m_RichText)); } else { // We are in "Ribbon" mode, so switch to "Command Bars" label.Text = SwitchToRibbon; // Create the command bars if (m_CommandBarBuilder == null) { m_CommandBarBuilder = new NRichTextCommandBarBuilder(); } SetUI(uiHolder, m_CommandBarBuilder.CreateUI(m_RichText)); } } #endregion #region Fields private NRichTextView m_RichText; private NRichTextRibbonBuilder m_RibbonBuilder; private NRichTextCommandBarBuilder m_CommandBarBuilder; #endregion #region Static Methods private static NParagraph GetDescriptionParagraph(string text) { return new NParagraph(text); } private static NParagraph GetTitleParagraphNoBorder(string text, int level) { double fontSize = 10; ENFontStyle fontStyle = ENFontStyle.Regular; switch (level) { case 1: fontSize = 16; fontStyle = ENFontStyle.Bold; break; case 2: fontSize = 10; fontStyle = ENFontStyle.Bold; break; } NParagraph paragraph = new NParagraph(); paragraph.HorizontalAlignment = ENAlign.Left; paragraph.FontSize = fontSize; paragraph.FontStyle = fontStyle; NTextInline textInline = new NTextInline(text); textInline.FontStyle = fontStyle; textInline.FontSize = fontSize; paragraph.Inlines.Add(textInline); return paragraph; } private static NGroupBlock GetDescriptionBlock(string title, string description, int level) { NColor color = NColor.Black; NParagraph paragraph = GetTitleParagraphNoBorder(title, level); NGroupBlock groupBlock = new NGroupBlock(); groupBlock.ClearMode = ENClearMode.All; groupBlock.Blocks.Add(paragraph); groupBlock.Blocks.Add(GetDescriptionParagraph(description)); groupBlock.Border = CreateLeftTagBorder(color); groupBlock.BorderThickness = DefaultBorderThickness; return groupBlock; } /// /// Creates a left tag border with the specified border /// /// /// private static NBorder CreateLeftTagBorder(NColor color) { NBorder border = new NBorder(); border.LeftSide = new NBorderSide(); border.LeftSide.Fill = new NColorFill(color); return border; } #endregion #region Constants private const string SwitchToCommandBars = "Switch to Command Bars"; private const string SwitchToRibbon = "Switch to Ribbon"; private static readonly NMargins DefaultBorderThickness = new NMargins(5.0, 0.0, 0.0, 0.0); #endregion #region Nested Types #endregion } }

Download a fully functional C# (.cs) Ribbon and Command Bars Example for WPF Solution

VB.NET Ribbon and Command Bars Example Code for Nevron RichTextEditor for WPF
Language: VB.NET

Imports Nevron.Nov
Imports Nevron.Nov.Dom
Imports Nevron.Nov.Graphics
Imports Nevron.Nov.Layout
Imports Nevron.Nov.Text
Imports Nevron.Nov.Text.UI
Imports Nevron.Nov.UI
Imports Nevron.Nov.Windows.Wpf

Class MainWindow

#Region "Constructors"

	Public Sub New()
		' This call is required by the designer.
		InitializeComponent()

		' Add any initialization after the InitializeComponent() call.

		' Set form title
		Title = "NOV WPF - NRibbonAndCommandBarsExample"

		' Create the NOV example and its controls
		Dim exampleContent As NWidget = CreateExampleContent()
		Dim exampleControls As NWidget = CreateExampleControls()
		If Not exampleControls Is Nothing Then
			exampleControls.PreferredWidth = 300
		End If

		' Place the example and its controls in a pair box
		Dim pairBox As NPairBox = New NPairBox(exampleContent, exampleControls)
		pairBox.FillMode = ENStackFillMode.First
		pairBox.FitMode = ENStackFitMode.First
		pairBox.Margins = New NMargins(NDesign.HorizontalSpacing, NDesign.VerticalSpacing)

		' Host the example in the form
		Dim host As NNovWidgetHost(Of NWidget) = New NNovWidgetHost(Of NWidget)(pairBox)
		Content = host
	End Sub


#End Region

#Region "Example"

        Public Function CreateExampleContent() As NWidget
            m_RichText = New NRichTextView()
            m_RichText.AcceptsTab = True
            m_RichText.Content.Sections.Clear()

            ' Populate the rich text
            PopulateRichText()

            ' Create and execute a ribbon UI builder
            m_RibbonBuilder = New NRichTextRibbonBuilder()
            Return m_RibbonBuilder.CreateUI(m_RichText)
        End Function
        Public Function CreateExampleControls() As NWidget
            ' Switch UI button
            Dim switchUIButton As NButton = New NButton(SwitchToCommandBars)
            switchUIButton.VerticalPlacement = ENVerticalPlacement.Top
            switchUIButton.Click += AddressOf OnSwitchUIButtonClick

            Return switchUIButton
        End Function
        Public Function GetExampleDescription() As String
            Return "

This example demonstrates how to add commanding interface to the NOV Rich Text Editor and how to switch between ribbon and command bars.

" End Function ''' ''' Gets the relevant documentation links for the example ''' ''' Public Function GetDocumentationUrls() As (String, String)() Return {("Rich Text Ribbon Overview", "https://www.nevron.com/docs/openvision/rich-text-ribbon-overview.html"), ("Rich Text Command Bars Overview", "https://www.nevron.com/docs/openvision/rich-text-command-bars-overview.html")} End Function Private Sub PopulateRichText() Dim section As NSection = New NSection() m_RichText.Content.Sections.Add(section) section.Blocks.Add(GetDescriptionBlock("Rich Text Ribbon and Command Bars", "This example demonstrates how to customize the NOV rich text ribbon.", 1)) End Sub #End Region #Region "Implementation" Private Sub SetUI(oldUiHolder As NCommandUIHolder, widget As NWidget) If TypeOf oldUiHolder.ParentNode Is NTabPage Then CType(oldUiHolder.ParentNode, NTabPage).Content = widget ElseIf TypeOf oldUiHolder.ParentNode Is NPairBox Then CType(oldUiHolder.ParentNode, NPairBox).Box1 = widget End If End Sub #End Region #Region "Event Handlers" Private Sub OnSwitchUIButtonClick(arg As NEventArgs) Dim switchUIButton = CType(arg.TargetNode, NButton) Dim label = CType(switchUIButton.Content, NLabel) ' Remove the rich text view from its parent Dim uiHolder As NCommandUIHolder = m_RichText.GetFirstAncestor(Of NCommandUIHolder)() m_RichText.ParentNode.RemoveChild(m_RichText) If Equals(label.Text, SwitchToRibbon) Then ' We are in "Command Bars" mode, so switch to "Ribbon" label.Text = SwitchToCommandBars ' Create the ribbon SetUI(uiHolder, m_RibbonBuilder.CreateUI(m_RichText)) Else ' We are in "Ribbon" mode, so switch to "Command Bars" label.Text = SwitchToRibbon ' Create the command bars If m_CommandBarBuilder Is Nothing Then m_CommandBarBuilder = New NRichTextCommandBarBuilder() End If SetUI(uiHolder, m_CommandBarBuilder.CreateUI(m_RichText)) End If End Sub #End Region #Region "Fields" Private m_RichText As NRichTextView Private m_RibbonBuilder As NRichTextRibbonBuilder Private m_CommandBarBuilder As NRichTextCommandBarBuilder #End Region #Region "Static Methods" Private Shared Function GetDescriptionParagraph(text As String) As NParagraph Return New NParagraph(text) End Function Private Shared Function GetTitleParagraphNoBorder(text As String, level As Integer) As NParagraph Dim fontSize As Double = 10 Dim fontStyle = ENFontStyle.Regular Select Case level Case 1 fontSize = 16 fontStyle = ENFontStyle.Bold Case 2 fontSize = 10 fontStyle = ENFontStyle.Bold End Select Dim paragraph As NParagraph = New NParagraph() paragraph.HorizontalAlignment = ENAlign.Left paragraph.FontSize = fontSize paragraph.FontStyle = fontStyle Dim textInline As NTextInline = New NTextInline(text) textInline.FontStyle = fontStyle textInline.FontSize = fontSize paragraph.Inlines.Add(textInline) Return paragraph End Function Private Shared Function GetDescriptionBlock(title As String, description As String, level As Integer) As NGroupBlock Dim color = NColor.Black Dim paragraph = GetTitleParagraphNoBorder(title, level) Dim groupBlock As NGroupBlock = New NGroupBlock() groupBlock.ClearMode = ENClearMode.All groupBlock.Blocks.Add(paragraph) groupBlock.Blocks.Add(GetDescriptionParagraph(description)) groupBlock.Border = CreateLeftTagBorder(color) groupBlock.BorderThickness = DefaultBorderThickness Return groupBlock End Function ''' ''' Creates a left tag border with the specified border ''' ''' ''' Private Shared Function CreateLeftTagBorder(color As NColor) As NBorder Dim border As NBorder = New NBorder() border.LeftSide = New NBorderSide() border.LeftSide.Fill = New NColorFill(color) Return border End Function #End Region #Region "Constants" Private Const SwitchToCommandBars As String = "Switch to Command Bars" Private Const SwitchToRibbon As String = "Switch to Ribbon" Private Shared ReadOnly DefaultBorderThickness As NMargins = New NMargins(5.0, 0.0, 0.0, 0.0) #End Region #Region "Nested Types" #End Region End Class

Download a fully functional VB.NET (.vb) Ribbon and Command Bars Example for WPF Solution