在Set detect back(shift)选项卡中

时间:2014-03-29 14:07:34

标签: .net wpf properties

在类的集合中,我无法访问Keyboard.IsKeyDown(Key.LeftShift)或Keyboard.Modifiers
属性是String

有没有办法检测后退/换档标签与前进标签相比? 换档标签和标签每个只发送一个普通标签到集合 发送的是前向选项卡,后面的选项卡没有Unicode(我可以找到)
所以我认为答案是否定的,但要求SO

我尝试使用转换器,因为转换器可以检测到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;
    }
}

这用于自动建议。 作为最后一个键的选项卡用于下一个建议。 想要使用之前建议的后退标签。 在这种情况下,Next和previous是合乎逻辑的。 之前基本上是排序Z-A的建议。 不寻找控制来为我这样做。 我需要检测集合中的后退标签。

2 个答案:

答案 0 :(得分:1)

作为'后退标签'作为character literal in C#不可用我只看到一个选项:

处理控件的键向下/向上事件,以将特殊序列(您自己的文字)发送到字符串。

当检测到特殊击键时,不要忘记将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而不是什么,但这似乎是你处理事件,编写行为或编写自定义控件的那些时候之一。

答案 1 :(得分:0)

本来可以保存自己100分 我只是使用转换器的错误侧 在设置之前调用ConvertBack,在set

之后调用Convert
[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;
    }
}
相关问题