RadWatermarkTextBox Selection is not shown when RadContextMenu is opened

5 posts, 1 answers
  1. Muhammad Ummar
    Muhammad Ummar avatar
    26 posts
    Member since:
    Jul 2012

    Posted 04 Jul 2014 Link to this post

    I am trying to add custom ContextMenu to RadWatermarkTextBox using RadContextMenu.ContextMenu attached property and disabling the default context menu. The custom context menu is working fine with the following XAML

    <telerik:RadWatermarkTextBox  Text="This is a test Text" Width="875" Height="70" ContextMenu="{x:Null}">
                        <telerik:RadContextMenu.ContextMenu>
                            <telerik:RadContextMenu >
                                <telerik:RadMenuItem Header="Copy" />
                                <telerik:RadMenuItem Header="Paste" />
                                <telerik:RadMenuItem Header="Cut" />
                                <telerik:RadMenuItem IsSeparator="True" />
                                <telerik:RadMenuItem Header="Select All" />
                            </telerik:RadContextMenu>
                        </telerik:RadContextMenu.ContextMenu>
                    </telerik:RadWatermarkTextBox>

    but the problem is, if I select some text and opens the context menu, the selection of RadWatermarkTextBox becomes clear as soon as context menu is open. Which doesn't look very good. And with default context menu this is not the behavior.

    I have tried following code in ContextMenu Opening event but no luck

    var tb = radContextMenu.GetClickedElement<RadWatermarkTextBox>();
    if (tb != null)
    {
       tb.Focus();
    }

    Please any suggestion for fix of this issue.
  2. Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 07 Jul 2014 Link to this post

    Hi Muhammad,

    When RadContextMenu is opened, it gets the focus of the application. So RadWatermarkTextBox goes to Unfocused state, which causes the loosing of the selection (only visual, if you check SelectedText property, you'll notice the selection is not lost at all). WPF's ContextMenu for TextBox is a special one, that doesn't get the focus. It uses some internal framework constructions in order to allow both selection in TextBox and ContextMenu to be focused. Our RadContextMenu is designed to support multiple scenarios, including Keyboard Navigation, placing it inside RadDropDownButton, etc. If we do not focus RadContextMenu when it is opened, you'll lose Keyboard Navigation and pressing any of the keyboard keys will close the menu.

    In order to achieve this case you can inherit RadContextMenu and override OnOpened method. In its body you shouldn't call base.OnOpened() and this way you'll have the menu opened and the selection in the watermark will be visible. Please note that using any of the keyboard keys will close the menu. You can handle KeyDown event of RadWatermarkTextBox and try to implement your own logic what should happen in situation where RadContextMenu is opened and the user had pressed one of the keyboard keys.

    Hope this helps.

    Regards,
    Rosen Vladimirov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. UI for WPF is Visual Studio 2017 Ready
  4. Muhammad Ummar
    Muhammad Ummar avatar
    26 posts
    Member since:
    Jul 2012

    Posted 07 Jul 2014 in reply to Rosen Vladimirov Link to this post

    Thanks Rosen,

    I tried the solution you have provided. and to some extent it is working fine, but not exactly what I want. The problem "When Context menu is opened the selection is gone" is SOLVED, but as soon as I move my mouse on one of the MenuItem in ConextMenu, the selection goes away.

    It looks like that MenuItem is now getting the focus?

    Could you please suggest some solution for it?

    Regards
    Ummar
  5. Answer
    Rosen Vladimirov
    Admin
    Rosen Vladimirov avatar
    640 posts

    Posted 07 Jul 2014 Link to this post

    Hi Ummar,

    Thank you for pointing this issue. In order to workaround this problem, you'll have to create your own class that inherits RadMenuItem and override OnMouseMove, OnMouseEnter and OnMouseLeave methods in the following way:
    class CustomMenuItem : RadMenuItem
    {
        protected override void OnMouseMove(System.Windows.Input.MouseEventArgs e)
        {
            this.IsHighlighted = true;
            this.ChangeVisualState(true);
        }
     
        protected override void OnMouseEnter(System.Windows.Input.MouseEventArgs e)
        {
            this.IsHighlighted = true;
            this.ChangeVisualState(true);
        }
     
        protected override void OnMouseLeave(System.Windows.Input.MouseEventArgs e)
        {
            this.IsHighlighted = false;
            this.ChangeVisualState(true);
        }
    }

    This way the highlighting will work fine. Also you'll have to override GetContainerForItem method of the CustomContextMenu and return a new instance of the new CustomMenuItem.

    I've modified the sample project to show you my idea. Please give it a try and inform us in case you have any problems or concerns.

    Regards,
    Rosen Vladimirov
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  6. Muhammad Ummar
    Muhammad Ummar avatar
    26 posts
    Member since:
    Jul 2012

    Posted 07 Jul 2014 in reply to Rosen Vladimirov Link to this post

    Thanks Rosen, It is working now.

    Regards
    Ummar
Back to Top
UI for WPF is Visual Studio 2017 Ready