Is it possible to bind a radrichtextbox command to it's context menu?

3 posts, 1 answers
  1. Michael
    Michael avatar
    15 posts
    Member since:
    Sep 2010

    Posted 15 Oct 2010 Link to this post

    I've been working on a text editor for a website using the radrichtextbox.  I have the radrichtextboxribbonui in place and working spectacularly, as well as a radMenu, that is working well.  I bound the cut, copy, and paste buttons in the radmenu to those of the radrichtextbox with the xaml code:
    <telerikNavigation:RadMenuItem x:Name="_cutMenu" DataContext="{Binding ElementName=_rrtbMessage}" Command="{Binding CutCommand}" Header="Cut" />
    <telerikNavigation:RadMenuItem x:Name="_copyMenu" DataContext="{Binding ElementName=_rrtbMessage}" Command="{Binding CopyCommand}" Header="Copy" />
    <telerikNavigation:RadMenuItem x:Name="_pasteMenu" DataContext="{Binding ElementName=_rrtbMessage}" Command="{Binding PasteCommand}" Header="Paste" />

    And that works well.  I tried to do the same thing with a context menu, and non of the buttons seem to work.  They simply respond as if they had no command.  The code I've written for that is:
                       
    <telerikDocuments:RadRichTextBox x:Name="_rrtbMessage" HorizontalAlignment="Stretch" Margin="10,35,10,10" VerticalAlignment="Stretch">
    <telerikDocuments:RadRichTextBox.Resources>
             <dHTML:HtmlDataProvider x:Key="HtmlDataProvider" RichTextBox="{ Binding ElementName=_rrtbMessage}" Html="{Binding MessageText, Mode=TwoWay}" SetupDocument="SetupMessage" />
    </telerikDocuments:RadRichTextBox.Resources>
    <telerikNavigation:RadContextMenu.ContextMenu>
    <telerikNavigation:RadContextMenu x:Name="_menuRtbFormat">
                            <telerikNavigation:RadMenuItem x:Name="_cutContextMenu" DataContext="{Binding ElementName=_rrtfMessage}" Command="{Binding CutCommand}" Header="Cut" />
                            <telerikNavigation:RadMenuItem x:Name="_copyContextMenu" DataContext="{Binding ElementName=_rrtfMessage}" Command="{Binding CopyCommand}" Header="Copy" />
                            <telerikNavigation:RadMenuItem x:Name="_pasteContextMenu" DataContext="{Binding ElementName=_rrtbMessage}" Command="{Binding PasteCommand}" Header="Paste"
    </telerikNavigation:RadContextMenu>
    </telerikNavigation:RadContextMenu.ContextMenu>
    </telerikDocuments:RadRichTextBox>

    Is there something I need to do differently because the context menu is nested inside of the RadRichTextbox I am trying to bind to?
  2. Answer
    Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 20 Oct 2010 Link to this post

    Hello Michael,

    I am attaching a project that shows two ways you can use to make the context menu work - in XAML and in code-behind.
    Basically, what you need to change in your code is not bind the DataContext property of the RadMenuItem, but the DataContext property of RadRichTextBox as follows:
    <telerikDocuments:RadRichTextBox x:Name="_rrtbMessage" HorizontalAlignment="Stretch"  Margin="10,35,10,10" DataContext="{Binding ElementName=_rrtbMessage, Path=Commands}">
    If you have any other questions, do not hesitate to contact us again.

    All the best,
    Iva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Michael
    Michael avatar
    15 posts
    Member since:
    Sep 2010

    Posted 20 Oct 2010 Link to this post

    Perfect, worked like a charm.
    Thanks,
    Mike
Back to Top