How to create RadRichTextBoxRibbonUI and RadRichTextBox in code behind

2 posts, 1 answers
  1. york
    york avatar
    160 posts
    Member since:
    Jul 2012

    Posted 10 Apr 2012 Link to this post

    I try to create a RadRichTextBoxRibbonUI and a RadRichTextBox. The xaml code is,

    <telerik:RadRichTextBox Grid.Row="1" HorizontalAlignment="Stretch" IsContextMenuEnabled="True" IsSelectionMiniToolBarEnabled="True" IsSpellCheckingEnabled="True" LayoutMode="Paged" Name="radRichTextBox1" />
    <telerik:RadRichTextBoxRibbonUI BackstageClippingElement="{Binding ElementName=gridRadRtbParent}" CollapseThresholdSize="50,50" DataContext="{Binding Path=Commands, ElementName=radRichTextBox1}" ApplicationName="Edit Letters">

    The C# code to dynamically create them is,

    RadRichTextBoxRibbonUI ribbonUI = CreateRibbonUI();
    ribbonUI.CollapseThresholdSize = new Size(50, 50);
    // Create a new RadRichTextBox
    RadRichTextBox richTextBox = new RadRichTextBox();
    richTextBox.Name = "radRichTextBox1";
    richTextBox.IsSpellCheckingEnabled = true;
    richTextBox.IsSelectionMiniToolBarEnabled = true;
    richTextBox.IsContextMenuEnabled = true;
    richTextBox.LayoutMode = DocumentLayoutMode.Paged;

    // Bind RadRichTextBox to RadRichTextBoxRibbonUI's datacontext
    binding = new Binding(richTextBox.Name);
    binding.Path = new PropertyPath("Commands");
    binding.Source = ribbonUI;
    ribbonUI.SetBinding(RadRichTextBoxRibbonUI.DataContextProperty, binding);

    However the code to bind RadRichTextBox to RadRichTextBoxRibbonUI's datacontext does not work because they are not binded. How to bind RadRichTextBoxRibbonUI and RadRichTextBox in C# that achieve the xaml code above?

  2. Answer
    Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 11 Apr 2012 Link to this post

    Hi York,

    We highly recommend that you declare the RibbonUI in XAML, as creating it in code-behind is rather inconvenient. Not only does it require a lot more code, but also it would be harder to achieve the desired layout.

    When it comes to code-behind declaration of the bindings, this can be done as follows:

    1. Binding the data context of RadRichTextBoxRibbonUI to the Commands property of RadRichTextBox:

    Binding binding = new Binding();
    binding.Source = richTextBox; //the instance of RadRichTextBox
    binding.Path = new PropertyPath("Commands");
    ribbonUI.SetBinding(RadRichTextBoxRibbonUI.DataContextProperty, binding);

    2. Binding the attached property RadRichTextBoxRibbonUI.RichTextCommand on a RadRibbonButton:
    RadRibbonButton boldButton = new RadRibbonButton();
    Binding binding = new Binding("ToggleBoldCommand");
    BindingOperations.SetBinding(boldButton, RadRichTextBoxRibbonUI.RichTextCommandProperty, binding);

    I hope this helps.

    All the best,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Back to Top