【问题标题】:Selection text disappears when contextmenu pops up弹出上下文菜单时选择文本消失
【发布时间】:2011-06-27 10:58:12
【问题描述】:

大家好,

当我创建一个以编程方式打开上下文菜单的事件时,我从 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 往往适用于解决方案。但谁能解释这是为什么?

【问题讨论】:

    标签: c# wpf textbox contextmenu selectedtext


    【解决方案1】:

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

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

    将 contextmenu 属性 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>
    

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

    【讨论】:

      【解决方案2】:

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

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

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

      另请查看answer 了解更多信息。

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

      【讨论】:

      • 嘿@Zamboni 感谢您的帮助,但我尽可能避免 e.Handled = true;呵呵:P 因为它通常会拒绝我访问其他文本框......虽然我确实尝试了一些效果很好但不知道如何解释它的东西。我设置了 contextmenu.focusable = false 并且似乎工作正常,我想知道您或其他任何人是否可以解释为什么会出现这种情况,或者它是否是一个合理的解决方案。谢谢男人
      【解决方案3】:

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

      很高兴听到,你得到了正确的答案。但这也可以 谢谢

      【讨论】:

        猜你喜欢
        • 2022-11-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-11
        • 2011-11-18
        • 1970-01-01
        • 2013-12-03
        • 1970-01-01
        相关资源
        最近更新 更多