开发者

Selection text disappears when contextmenu pops up

开发者 https://www.devze.com 2023-02-11 15:34 出处:网络
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

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.focu开发者_运维知识库sable = false tends to work with the solution. but can anybody explain why is that?


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


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;
}


Simply, Properties > Hide Selection = False

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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号