C# 带有不接受星号(NumPad)的文本框的TreeView
我有一个树状视图,其中包含一对文本框,当我尝试从NumPad输入星号时,它不起作用(Shift+8起作用) 我一直在四处寻找,发现星号在默认情况下会触发树视图中的操作C# 带有不接受星号(NumPad)的文本框的TreeView,c#,wpf,xaml,textbox,treeview,C#,Wpf,Xaml,Textbox,Treeview,我有一个树状视图,其中包含一对文本框,当我尝试从NumPad输入星号时,它不起作用(Shift+8起作用) 我一直在四处寻找,发现星号在默认情况下会触发树视图中的操作 有没有办法让文本框接受星号而不触发默认的treeview操作?要让文本框接受星号而不触发默认的treeview操作,请使用内置的WPF输入绑定。使用键设置KeyBinding到ApplicationCommands.nota命令。将作为键手势。在TreeView级别设置键绑定,如下所示 <TreeView.InputBind
有没有办法让文本框接受星号而不触发默认的treeview操作?要让文本框接受星号而不触发默认的treeview操作,请使用内置的WPF
输入绑定。使用键设置KeyBinding
到ApplicationCommands.nota命令
。将
作为键手势。在TreeView级别设置键绑定,如下所示
<TreeView.InputBindings>
<KeyBinding Gesture="Multiply" Command="ApplicationCommands.NotACommand" />
</TreeView.InputBindings>
请注意,这将禁用绑定到键的命令。乘法
,即NumPad星号。要引用ApplicationCommand.NotACommand
属性上的页面,请执行以下操作:
此命令始终被忽略,并且不处理导致它的输入事件。这提供了一种关闭现有控件中内置的输入绑定的方法
下面是我根据msdn上的回复编写的一些代码。这是用于numpad中的“+”和“-”,你可以用星号做同样的事情
private void UIElement_OnKeyDown(object sender, KeyEventArgs e)
{
var textbox = e.OriginalSource as TextBox;
if (textbox != null && (e.Key == Key.Add || e.Key == Key.Subtract))
{
string signToInsert = e.Key == Key.Add ? "+" : "-";
if (textbox.SelectionLength == 0)
{
int caretPosition = textbox.CaretIndex;
textbox.Text = textbox.Text.Insert(caretPosition, signToInsert);
textbox.CaretIndex = caretPosition + 1;
}
else
{
int selectionStart = textbox.SelectionStart;
int selectionLength = textbox.SelectionLength;
string newText = "";
newText += textbox.Text.Substring(0, selectionStart);
newText += signToInsert;
newText += textbox.Text.Substring(selectionStart + selectionLength);
textbox.Text = newText;
textbox.CaretIndex = selectionStart + 1;
}
e.Handled = true;
}
}