问候全部,
当我创建一个以编程方式打开上下文菜单的事件时,我从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往往与解决方案一起使用。但有人可以解释为什么会这样吗?
答案 0 :(得分:1)
每当你打开上下文菜单时,焦点都会转到上下文菜单, 这就是文本框隐藏选择的原因。
此问题的简单解决方案是:
将contextmenu属性可聚焦更改为false
<ContextMenu Focusable="False">
并将每个项目的可聚焦更改为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>
这样焦点将保留在文本框中, 并且突出显示将保持可见
答案 1 :(得分:0)
我可以帮助您入门,但此解决方案存在一些您可能需要克服的严重可用性问题。
此解决方案意味着当您可能想要取消选择其值时,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;
}
答案 2 :(得分:0)
简单地说, 属性&gt;隐藏选择=假
很高兴听到,你得到了正确的答案。但这简直就行了 感谢