弹出上下文菜单时,选择文本消失 [英] Selection text disappears when contextmenu pops up

查看:139
本文介绍了弹出上下文菜单时,选择文本消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候所有,
当我创建一个事件以编程方式打开上下文菜单时,我从WPF中得到了一些奇怪的行为.一旦我选择了文本并右键单击,上下文菜单打开后,所选内容的高亮部分便消失了.
这是问题的一个示例:

Xaml:


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

推荐答案

WPF控件没有HideSelection属性,但是您可以查看这些链接.也许可以为您解决.
http://social.msdn.microsoft.com/Forums/zh/wpf/thread/0f0858ed-eb14-4b3d-8d39-88034bc096f4 [ http://social.msdn.microsoft.com /forums/zh-CN/wpf/thread/ac968cc6-d71d-4710-affd-8fec14085017/ [
There is no HideSelection property for WPF controls but you can have a look at these links. Maybe that could work out for you.
http://social.msdn.microsoft.com/Forums/en/wpf/thread/0f0858ed-eb14-4b3d-8d39-88034bc096f4[^]

http://social.msdn.microsoft.com/forums/en-US/wpf/thread/ac968cc6-d71d-4710-affd-8fec14085017/[^]

Good luck!


这篇关于弹出上下文菜单时,选择文本消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆