0

Greetings All,

I am getting some weird behavior from WPF when i create an event to programatically open a context menu. once I select a text and right click the highlight of the selection disappears once the context menu opens up.

Here is a sample of the problem:

Xaml:

<Window x:Class="WpfApplication19.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="287" Width="419">
    <Grid>
        <TextBox x:Name="myText" Height="38" Margin="0,72,6,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="135"></TextBox>
        <Label Height="30" Margin="12,80,164,0" Name="label1" VerticalAlignment="Top">Textbox with contextMenu property set</Label>
        <Label Height="30" Margin="12,0,136,91" Name="label2" VerticalAlignment="Bottom">TextBox Open ContextMenu by programmatically</Label>
        <TextBox Height="38" Margin="0,0,6,81" x:Name="myText1" VerticalAlignment="Bottom" HorizontalAlignment="Right" Width="135" />
    </Grid>
</Window>

And the Code Behind:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication19
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class Window1 : Window
    {
        ContextMenu con = new ContextMenu();
        public Window1()
        {
            InitializeComponent();
            MenuItem menuItem1 = new MenuItem();
            menuItem1.Header = "Menu1";
            MenuItem menuItem2 = new MenuItem();
            menuItem2.Header = "Menu2";

            con.Items.Add(menuItem1);
            con.Items.Add(menuItem2);

            this.myText.ContextMenu = con;

            this.myText1.PreviewMouseDown += new MouseButtonEventHandler(myText1_PreviewMouseDown);
        }

        void myText1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            base.OnPreviewMouseDown(e);
            if (e.RightButton == MouseButtonState.Pressed)
            {
               con.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;

               con.IsOpen = true;
               IInputElement focusedElement = FocusManager.GetFocusedElement(this); 
            }
        }
    }
}

Thank you in advance!

Note: I found out that adding con.focusable = false tends to work with the solution. but can anybody explain why is that?

Nervous_cat
  • 19
  • 4
  • 9

3 Answers3

1

Whenever u open the contextmenu the focus will go to the contextmenu, and that's why the textbox will hide the selection.

A Simple solution for this problem is:

Change contextmenu property Focusable to false

<ContextMenu Focusable="False">

And change Focusable for every item to false

<MenuItem Command="Copy" Focusable="False">


Simple example:

<TextBox Text="Right-click here for context menu!">
    <TextBox.ContextMenu>
        <ContextMenu Focusable="False">
            <MenuItem Command="Cut" Focusable="False"/>
            <MenuItem Command="Copy" Focusable="False"/>
            <MenuItem Command="Paste" Focusable="False"/>
        </ContextMenu>
    </TextBox.ContextMenu>
</TextBox>

This way the focus will stay on the textbox, and the highlight will remain Visible

Strider
  • 4,452
  • 3
  • 24
  • 35
0

Simply, Properties > Hide Selection = False

Its nice to hear, you got the right answer. but simply this will also works Thanks

0

I can get you started, but this solution has some serious usability issues that you may need to overcome.

  1. Add a LostFocus event handler to control myText.
  2. Set myText1.Focus() during the right-click event so that you can trigger the LostFocus event.

This solution means that myText stays selected when you might want to unselect its value.

Also take at look at this answer for more information.

<TextBox LostFocus="myText_LostFocus" x:Name="myText" Height="38" Margin="0,72,6,0" VerticalAlignment="Top" HorizontalAlignment="Right" Width="135"></TextBox>

void myText1_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
  base.OnPreviewMouseDown(e);
  if (e.RightButton == MouseButtonState.Pressed)
  {
    // added next line
    myText1.Focus();
    con.Placement = System.Windows.Controls.Primitives.PlacementMode.MousePoint;

    con.IsOpen = true;
    IInputElement focusedElement = FocusManager.GetFocusedElement(this);
  }
}

private void myText_LostFocus(object sender, RoutedEventArgs e)
{
  e.Handled = true;
}
Community
  • 1
  • 1
Zamboni
  • 7,897
  • 5
  • 43
  • 52
  • hey @Zamboni thanks for the help, but am trying as much as possible to avoid e.Handled = true; hehe :P since it usually denies me access into other textboxes... though i did try something that worked well but am not sure how to interpret it. I set contextmenu.focusable = false and seemed to work fine, I was wondering if you or anyone else casn interpret why this is appening or if it a plausible solution. thx man – Nervous_cat Feb 21 '11 at 15:50