我可以绑定到实用程序类吗?

时间:2015-03-11 04:13:32

标签: c# windows-phone-8 mvvm windows-phone

我有一个公共类,其中填充了下面的通用函数来解析文本框:

public static void DoubleParse_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Decimal)
    {
        var textBox = sender as TextBox;
        if (textBox != null)
            textBox.Text += Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);
    }
    else
    {
        e.Handled = (e.Key >= Key.D0 && e.Key <= Key.D9) ||
                    (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || 
                    e.Key == Key.Back || e.Key == Key.Delete ||
                    e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Unknown;
    }
}

我以为我可以在我的网页上随处使用它作为TextBox keydown事件的单一来源。 WP8中MVVM实现的新手,好奇是否有办法实现这一目标?

本着MVVM的精神(尽管我不是纯粹主义者),我明白它并不需要特别是在viewmodel中,但我仍然喜欢它集中化。

快速说明:

  • 该类不是静态的,我知道我不能直接在xaml中使用它。
  • 我想让类成为某种StaticResource并引用xaml中的函数。但这似乎不起作用。
  • 我目前只是在代码隐藏中使用传递函数并将发送者传递给静态函数。

1 个答案:

答案 0 :(得分:3)

您想要一个附加行为。

public static class TextBoxBehavior
{
    public static bool GetAllowOnlyDecimalInput(TextBox texbox)
    {
        return (bool)texbox.GetValue(AllowOnlyDecimalInputProperty);
    }

    public static void SetAllowOnlyDecimalInput(
      TextBox texbox, bool value)
    {
        texbox.SetValue(AllowOnlyDecimalInputProperty, value);
    }

    public static readonly DependencyProperty AllowOnlyDecimalInputProperty =
        DependencyProperty.RegisterAttached(
        "AllowOnlyDecimalInput",
        typeof(bool),
        typeof(TextBox),
        new PropertyMetadata(false, OnAllowOnlyDecimalInputChanged));

    static void OnAllowOnlyDecimalInputChanged(
      DependencyObject depObj, DependencyPropertyChangedEventArgs e)
    {
        TextBox item = depObj as TextBox;
        if (item == null)
            return;

        if (e.NewValue is bool == false)
            return;

        if ((bool)e.NewValue)
            item.KeyDown += OnTextBoxDoubleParse_KeyDown;
        else
            item.KeyDown -= OnTextBoxDoubleParse_KeyDown;
    }

    static void OnTextBoxDoubleParse_KeyDown(object sender, KeyEventArgs e)
    {
        if (!Object.ReferenceEquals(sender, e.OriginalSource))
            return;

        TextBox item = e.OriginalSource as TextBox;
        if (item != null) {
            if (e.Key == Key.Decimal)
            {
                var textBox = sender as TextBox;
                if (textBox != null)
                    textBox.Text += Convert.ToChar(CultureInfo.CurrentCulture.NumberFormat.CurrencyDecimalSeparator);
            }
            else
            {
                e.Handled = (e.Key >= Key.D0 && e.Key <= Key.D9) ||
                            (e.Key >= Key.NumPad0 && e.Key <= Key.NumPad9) || 
                            e.Key == Key.Back || e.Key == Key.Delete ||
                            e.Key == Key.Left || e.Key == Key.Right || e.Key == Key.Unknown;
            }
        }
    }

    #endregion // AllowOnlyDecimalInput
}

在XAML中使用

<TextBox my:TextBoxBehavior.AllowOnlyDecimalInput="True" />

您也可以在WPF样式中设置它,并在所有或多个控件中重复使用它,而不是每次手动添加属性。