弹出上下文菜单时选择文本消失

发布于 2024-10-17 10:55:45 字数 2648 浏览 1 评论 0原文

大家好,

当我创建一个事件以编程方式打开上下文菜单时,我从 WPF 中得到了一些奇怪的行为。一旦我选择一个文本并右键单击,一旦上下文菜单打开,所选内容的突出显示就会消失。

这是问题的示例:

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>

以及背后的代码:

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

提前谢谢您!

笔记: 我发现添加 con.focusable = false 往往可以解决该问题。但有人能解释这是为什么吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无法言说的痛 2024-10-24 10:55:46

每当您打开上下文菜单时,焦点都会转到上下文菜单,
这就是文本框将隐藏选择的原因。

此问题的一个简单解决方案是:

将上下文菜单属性 Focusable 更改为 false

<ContextMenu Focusable="False">

并将每个项目的 Focusable 更改为 false

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

简单示例:

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

这样焦点将停留在文本框上,
并且突出显示将保持可见

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

无尽的现实 2024-10-24 10:55:46

我可以帮助您入门,但此解决方案存在一些您可能需要克服的严重可用性问题。

  1. 添加 LostFocus 事件处理程序来控制 myText。
  2. 在右键单击事件期间设置 myText1.Focus(),以便可以触发 LostFocus 事件。

此解决方案意味着当您可能想要取消选择 myText 的值时,它会保持选中状态。

另请参阅此答案了解更多信息。

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

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;
}
今天小雨转甜 2024-10-24 10:55:46

简单地,
属性>隐藏选择 = False

很高兴听到您得到了正确的答案。但这也行得通
谢谢

Simply,
Properties > Hide Selection = False

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文