.net 在“设置后检测(移位)”选项卡中
在类中的集合中,我无法访问Keyboard.IsKeyDown(Key.LeftShift)或Keyboard.Modifiers.net 在“设置后检测(移位)”选项卡中,.net,wpf,properties,.net,Wpf,Properties,在类中的集合中,我无法访问Keyboard.IsKeyDown(Key.LeftShift)或Keyboard.Modifiers 属性是字符串 是否有一种方法可以与前进选项卡相比检测后退/移位选项卡? shift选项卡和tab选项卡只向集合发送一个普通选项卡。 发送的是向前选项卡,向后选项卡没有Unicode(我可以找到) 因此,我认为答案是否定的,只是继续问下去 我尝试使用一个转换器,因为在转换器中可以检测到shift键并将选项卡更改为其他内容,但问题是设置后调用转换器 [ValueConv
属性是字符串 是否有一种方法可以与前进选项卡相比检测后退/移位选项卡?
shift选项卡和tab选项卡只向集合发送一个普通选项卡。
发送的是向前选项卡,向后选项卡没有Unicode(我可以找到)
因此,我认为答案是否定的,只是继续问下去 我尝试使用一个转换器,因为在转换器中可以检测到shift键并将选项卡更改为其他内容,但问题是设置后调用转换器
[ValueConversion(typeof(String), typeof(String))]
public class StringTabConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
String strValue = (String)value;
if (string.IsNullOrEmpty(strValue)) return strValue;
if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
{
System.Diagnostics.Debug.WriteLine("shift");
char? lastChar = null;
lastChar = strValue.ToCharArray()[strValue.Length-1];
if (lastChar != null && (char)lastChar == '\t')
{
strValue = strValue.Replace('\t','~'); //'\v'
}
}
return strValue;
// can change it here but of no value as this is after the set
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = (String)value;
return strValue;
}
}
用于自动建议。
最后一个键是下一个建议的选项卡。
要使用“上一步”选项卡获取先前的建议。
在这种情况下,“下一个”和“上一个”是合乎逻辑的。
Prior基本上是排序Z-A建议。
不想找一个控件来帮我做这件事。
我需要检测集合中的back tab。因为“back tab”不可用,我只看到一个选项: 处理控件的键向下/向上事件,以向字符串发送特殊序列(您自己的文本) 当检测到特殊按键时,不要忘记将
e.Handled
设置为true
public class TextBoxEx : TextBox
{
protected override void OnKeyUp(System.Windows.Input.KeyEventArgs e)
{
if(e.Key == System.Windows.Input.Key.Tab &&
e.KeyboardDevice.Modifiers == ModifierKeys.Shift)
{
//save the current position
var previousPosition = SelectionStart;
//insert the magic value at the current position
Text = Text.Insert(previousPosition, "@");
//set the current position correctly because
Select(previousPosition+1, 0);
//we skip the default handling
e.Handled = true;
}
}
}
请注意,这不会处理特殊情况,例如用户选择几个字符,然后键入Shift+Tab,但您必须定义在这种情况下应该发生什么,并且可以轻松添加
我知道这是针对MVVM的,但这似乎是处理事件、编写行为或编写自定义控件的时候之一 本可以帮我省下100分
我只是使用了转换器的错误一侧
ConvertBack在set之前调用,Convert在set之后调用
[ValueConversion(typeof(String), typeof(String))]
public class StringTabConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
String strValue = (String)value;
System.Diagnostics.Debug.WriteLine(string.Empty);
System.Diagnostics.Debug.WriteLine("Convert " + strValue);
//if (string.IsNullOrEmpty(strValue)) return strValue;
return strValue;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
string strValue = (String)value;
System.Diagnostics.Debug.WriteLine(string.Empty);
System.Diagnostics.Debug.WriteLine("ConvertBack " + strValue);
if (string.IsNullOrEmpty(strValue)) return strValue;
if ((Keyboard.Modifiers & ModifierKeys.Shift) > 0)
{
System.Diagnostics.Debug.WriteLine("shift");
char? lastChar = null;
char[] charArray = strValue.ToCharArray();
lastChar = charArray[charArray.Length - 1];
if (lastChar != null && (char)lastChar == '\t')
{
//strValue = strValue.Replace('\t', '~'); //'\v'
StringBuilder sb = new StringBuilder();
for (int i = 0; i < charArray.Length - 1; i++) sb.Append(charArray[i]);
sb.Append('~');
strValue = sb.ToString();
System.Diagnostics.Debug.WriteLine("ConvertBack replace " + strValue);
}
}
return strValue;
}
}
[ValueConversion(typeof(String),typeof(String))]
公共类StringTabConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、CultureInfo区域性)
{
字符串strValue=(字符串)值;
System.Diagnostics.Debug.WriteLine(string.Empty);
System.Diagnostics.Debug.WriteLine(“转换”+strValue);
//if(string.IsNullOrEmpty(strValue))返回strValue;
返回标准值;
}
公共对象转换回(对象值、类型targetType、对象参数、CultureInfo区域性)
{
字符串strValue=(字符串)值;
System.Diagnostics.Debug.WriteLine(string.Empty);
System.Diagnostics.Debug.WriteLine(“ConvertBack”+strValue);
if(string.IsNullOrEmpty(strValue))返回strValue;
如果((Keyboard.Modifiers和ModifierKeys.Shift)>0)
{
系统.诊断.调试.写入线(“shift”);
char?lastChar=null;
char[]charArray=strValue.ToCharArray();
lastChar=charArray[charArray.Length-1];
如果(lastChar!=null&&(char)lastChar=='\t')
{
//strValue=strValue.Replace('\t','~');//'\v'
StringBuilder sb=新的StringBuilder();
对于(inti=0;i
如何更改按键按下事件中的值?e、 键被读取only@Blam-请勿更改钥匙。更改字符串/文本属性。通过设置e.Handled=true代码>修改文本属性后,您将阻止默认添加到文本属性。@BARM-我添加了一个如何创建自定义文本框的示例。我感谢您的时间和回答,但正如问题中所述,我不想寻找控件解决方案。我想出来了。但是奖金点数不属于我,所以你很可能会得到+100,我对此很满意。谢谢你没有得到赏金。那不是我的意图。我打了个旗子,让他们给你赏金。老实说,我认为这是错误的。转换器不应该知道键盘状态。单元测试这很难,因为它依赖于外部状态。在视图中处理用户交互。在转换器中处理(只是)转换,并在ViewModel中执行所有其他(数据)操作:)当然可以。我理解工作守则的吸引力。我只是添加了我在编写软件时需要考虑的额外因素:设计(责任)和质量(自动测试性)@ernodeweard无意与您争论。但即使在另一个页面上,转换器也能感知键盘状态。即使转换器位于另一个页面上,此操作也有效。如果我将转换器放在纯类中,那么当我尝试从窗口尊重它时,甚至找不到它。我在对OnKeyUp进行单元测试时也会遇到同样的问题。别担心,这些都是有效的问题。我主要关心的是测试转换器,而不是视图。一般来说,我不测试视图自动化(有太多的依赖项和更改),但我确实想测试转换器和视图模型。@erNodeWeard我不在那个环境中。事实上,我有一些讨厌的用户体验人员想要成为数据设计师。他们希望控件做的事情在数据端是不可伸缩的(对我来说也是不符合逻辑的)。如果我搞砸了,他们会声称这是他们的地盘。