Multiple RichTextBoxes Paste Error

3 posts, 1 answers
  1. Anthony
    Anthony avatar
    10 posts
    Member since:
    Feb 2009

    Posted 23 Nov 2012 Link to this post

    I've run in to some strange behaviour when I have two or more RadRichTextBoxes on the screen at the same time while using a custom ContextMenuContentBuilder. When I click on Paste from the context menu of any of the RadRichTextBoxes it pastes in the last one rendered. However if I paste using CTRL+V it works properly. I'm guessing I've implemented something wrong here but can't quite figure out what.

    Here is the code:
    MainPage.xaml

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel>
            <telerik:RadRichTextBox Height="100" x:Name="RadRichTextBox1">
            </telerik:RadRichTextBox>
            <telerik:RadRichTextBox Height="100" x:Name="RadRichTextBox2">
            </telerik:RadRichTextBox>
     
        </StackPanel>
    </Grid>

    MainPage.xaml.cs

    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            Loaded += new RoutedEventHandler(MainPage_Loaded);
        }
     
        void MainPage_Loaded(object sender, RoutedEventArgs e)
        {
            if (!DesignerProperties.IsInDesignTool)
            {
                ContextMenu contextMenu1 = (ContextMenu)RadRichTextBox1.ContextMenu;
                contextMenu1.ContentBuilder = new TextMenuBuilder(RadRichTextBox1);
     
                ContextMenu contextMenu2 = (ContextMenu)RadRichTextBox2.ContextMenu;
                contextMenu2.ContentBuilder = new TextMenuBuilder(RadRichTextBox2);
            }
     
        }
    }

    TextMenuBuilder.cs

    public class TextMenuBuilder:ContextMenuContentBuilder
    {
        private RadRichTextBox radRichTextBox;
        public TextMenuBuilder(RadRichTextBox radRichTextBox)
            :base(radRichTextBox)
        {
            this.radRichTextBox = radRichTextBox;
        }
    }

    By the way, I've taken all of my custom code out of the TextMenuBuilder class in an attempt to isolate the issue - I am planning to add custom menu items to each of the different RadRichTextBoxes.

    Thanks,
    Anthony

  2. Answer
    Boby
    Admin
    Boby avatar
    595 posts

    Posted 26 Nov 2012 Link to this post

    Hello Anthony,
    The behavior you are observing is due to the fact that by default ContextMenu instance is shared between all the RadRichTextBoxes. You can easily change the default context menu by  declaring non-sharable custom menu and mark it with CustomContextMenuAttribute, so that RadRichTextBoxes could automatically load it:
    [PartCreationPolicy(CreationPolicy.NonShared)]
    [CustomContextMenu]
    public class NonSharedContextMenu : ContextMenu
    {
     
    }

    Don't hesitate to contact us if you have further questions.



    All the best,
    Boby
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft banner
  4. Anthony
    Anthony avatar
    10 posts
    Member since:
    Feb 2009

    Posted 26 Nov 2012 Link to this post

    Thanks Boby,

    I was hoping it was something simple like that! It worked perfectly!

    Thanks,
    Anthony
Back to Top