2017-08-21 17 views
1

問題は簡単です。WPFに数値を入力できるテキストボックスがあります。私は特定のキーを押したとき、私は3つのことをしたい:TextBoxの添付プロパティWPF

  • を入力します。値が送信される
  • 最大:値が増加
  • ダウンを送信されます。値が

を、その後減少送られます私は3つの添付プロパティを作成しましたが、キー入力の場合にのみ機能します。

<TextBox Margin="5,0" hlp:TextboxBehaviors.KeyToValid="Enter" hlp:Decrease.KeyToDecrease="Down" hlp:Increase.KeyToIncrease="Up" Text="{Binding Target, Mode=TwoWay, UpdateSourceTrigger=Explicit, Converter={StaticResource kiloConverter}, ValidatesOnNotifyDataErrors=True, ValidatesOnExceptions=True}" /> 

クラスのコード:

public static class Decrease 
{ 
    private static Key TriggeredKey = Key.Down; 
    public static Key GetKeyToDecrease(DependencyObject obj) 
    { 
     return (Key)obj.GetValue(KeyToDecreaseProperty); 
    } 

    /// <summary> 
    /// Sert uniquement à s'attacher à la TextBox (une seule fois à l'init de l'interface) 
    /// </summary> 
    public static void SetKeyToDecrease(DependencyObject obj, Key value) 
    { 
     var tb = obj as TextBox; 
     if (tb != null) 
      tb.KeyDown += OnKeyDown; 
    } 

    /// <summary> 
    /// Evenement classique 
    /// </summary> 
    private static void OnKeyDown(object sender, KeyEventArgs e) 
    { 
     var tbCurrent = sender as TextBox; 
     int selectedpos; 

     if (tbCurrent != null && e.Key == TriggeredKey) 
     { 
      if (tbCurrent.SelectionStart > 0) 
      { 
       if (!tbCurrent.Text.Contains(".")) 
       { 
        int exp = tbCurrent.Text.Length - tbCurrent.SelectionStart; 
        tbCurrent.Text = (Convert.ToDouble(tbCurrent.Text) - Math.Pow(10, exp)).ToString(); 
        selectedpos = tbCurrent.Text.Length - exp; 
       } 
       else 
       { 
        int exp; 
        if (tbCurrent.SelectionStart > tbCurrent.Text.IndexOf('.') + 1) 
        { 
         exp = tbCurrent.Text.IndexOf('.') - tbCurrent.SelectionStart + 1; 
        } 
        else if (tbCurrent.SelectionStart < tbCurrent.Text.IndexOf('.') + 1) 
        { 
         exp = tbCurrent.Text.IndexOf('.') - tbCurrent.SelectionStart; 
        } 
        else 
        { 
         return; 
        } 

        tbCurrent.Text = (Convert.ToDouble(tbCurrent.Text) - Math.Pow(10, exp)).ToString("0.00"); 

        if (exp < 0) 
        { 
         selectedpos = tbCurrent.Text.IndexOf('.') - exp + 1; 
        } 
        else 
        { 
         if (tbCurrent.Text.IndexOf('.') - exp < 0) 
         { 
          selectedpos = 0; 
         } 
         else 
         { 
          selectedpos = tbCurrent.Text.IndexOf('.') - exp; 
         } 
        } 
       } 

       BindingExpression binding = tbCurrent.GetBindingExpression(TextBox.TextProperty); 
       if (binding != null) 
       { 
        binding.UpdateSource(); 
       } 

       tbCurrent.SelectionLength = 0; 
       if (selectedpos >= 0) 
       { 
        tbCurrent.SelectionStart = selectedpos; 
       } 
       else 
       { 
        tbCurrent.SelectionStart = 0; 
       } 
      } 
     } 
    } 

    // Using a DependencyProperty as the backing store for KeyToValid. This enables animation, styling, binding, etc... 
    public static readonly DependencyProperty KeyToDecreaseProperty = 
     DependencyProperty.RegisterAttached("KeyToDecrease", typeof(Key), typeof(TextBox), new PropertyMetadata(TriggeredKey)); 

} 

この二他の人が同じように設計されて減少添付の動作のためのコード。

イベントがトリガーされないようにしてください。エンターテイメントは...

答えて

0

イベントがトリガーされません! ...

プレビュー KeyDownイベントを処理しない入力します。独自に

public static void SetKeyToDecrease(DependencyObject obj, Key value) 
{ 
    var tb = obj as TextBox; 
    if (tb != null) 
     tb.PreviewKeyDown += OnKeyDown; 
} 

TextBoxコントロールハンドルと「ツバメ」特定のキーを押します。そのため、あなたの行動ではKeyDownイベントが発生しません。