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

RadWatermarkTextBox Selection is not shown when RadContextMenu is opened

4 Answers 202 Views
ContextMenu
This is a migrated thread and some comments may be shown as answers.
Muhammad Ummar
Top achievements
Rank 1
Muhammad Ummar asked on 04 Jul 2014, 07:04 AM
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.

4 Answers, 1 is accepted

Sort by
0
Rosen Vladimirov
Telerik team
answered on 07 Jul 2014, 11:32 AM
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.
 
0
Muhammad Ummar
Top achievements
Rank 1
answered on 07 Jul 2014, 11:57 AM
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
0
Accepted
Rosen Vladimirov
Telerik team
answered on 07 Jul 2014, 12:22 PM
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.
 
0
Muhammad Ummar
Top achievements
Rank 1
answered on 07 Jul 2014, 01:09 PM
Thanks Rosen, It is working now.

Regards
Ummar
Tags
ContextMenu
Asked by
Muhammad Ummar
Top achievements
Rank 1
Answers by
Rosen Vladimirov
Telerik team
Muhammad Ummar
Top achievements
Rank 1
Share this question
or