This is a migrated thread and some comments may be shown as answers.

How to create RadRichTextBoxRibbonUI and RadRichTextBox in code behind

1 Answer 257 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
york
Top achievements
Rank 1
york asked on 10 Apr 2012, 07:05 AM
Hi,
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?

Thanks,
York

1 Answer, 1 is accepted

Sort by
0
Accepted
Iva Toteva
Telerik team
answered on 11 Apr 2012, 11:17 AM
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 >>

Tags
RichTextBox
Asked by
york
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Share this question
or