C# TextBox WPF上的附加属性

C# TextBox WPF上的附加属性,c#,wpf,C#,Wpf,我的问题很简单,我在WPF中有一个文本框,您可以在其中键入数字。当我按特定键时,我需要三件事: 输入:发送值 向上:值增加后发送 向下:值减小后发送 我有3个附加属性,但它只适用于键输入 <TextBox Margin="5,0" hlp:TextboxBehaviors.KeyToValid="Enter" hlp:Decrease.KeyToDecrease="Down" hlp:Increase.KeyToIncrease="Up" Text="{Binding Target

我的问题很简单,我在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));

}
公共静态类减少
{
私有静态密钥TriggeredKey=Key.Down;
公共静态密钥GetKeyToDecrease(DependencyObject obj)
{
返回(键)对象GetValue(键到DecreaseProperty);
}
/// 
///在文本框中插入一个附件(在界面的初始位置)
/// 
公共静态无效SetKeyToDecrese(DependencyObject对象,键值)
{
var tb=对象作为文本框;
如果(tb!=null)
tb.KeyDown+=OnKeyDown;
}
/// 
///古典黄昏
/// 
私有静态void OnKeyDown(对象发送方,KeyEventArgs e)
{
var tbCurrent=发送方作为文本框;
int-selectedpos;
if(tbCurrent!=null&&e.Key==TriggeredKey)
{
如果(tbCurrent.SelectionStart>0)
{
如果(!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;
}
其他的
{
国际贸易;
if(tbCurrent.SelectionStart>tbCurrent.Text.IndexOf('.')+1)
{
exp=tbCurrent.Text.IndexOf('.')-tbCurrent.SelectionStart+1;
}
else if(tbCurrent.SelectionStart=0)
{
tbCurrent.SelectionStart=selectedpos;
}
其他的
{
tbCurrent.SelectionStart=0;
}
}
}
}
//使用DependencyProperty作为KeyToValid的后备存储。这将启用动画、样式、绑定等。。。
public static readonly dependencProperty KeyToDecreaseProperty=
DependencyProperty.RegisterAttached(“KeyToDecrease”、typeof(Key)、typeof(TextBox)、newpropertyMetadata(TriggeredKey));
}
这是减少附加行为的代码,其他两个也是以相同的方式设计的

减少和增加不会触发事件!输入不

减少和增加不会触发事件!输入不

处理预览按键事件:

public static void SetKeyToDecrease(DependencyObject obj, Key value)
{
    var tb = obj as TextBox;
    if (tb != null)
        tb.PreviewKeyDown += OnKeyDown;
}
TextBox
控制自己处理和“吞咽”某些按键。这就是为什么在您的行为中不会触发
KeyDown
事件的原因

减少和增加不会触发事件!输入不

处理预览按键事件:

public static void SetKeyToDecrease(DependencyObject obj, Key value)
{
    var tb = obj as TextBox;
    if (tb != null)
        tb.PreviewKeyDown += OnKeyDown;
}
TextBox
控制自己处理和“吞咽”某些按键。这就是为什么在您的行为中不会触发
KeyDown
事件的原因