我可以绑定到实用程序类吗? [英] Can I bind to a utility class?

查看:77
本文介绍了我可以绑定到实用程序类吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个通用类,其中填充了通用功能,例如下面的功能来解析文本框:

I have a common class filled with generic functions like the one below to parse a text box:

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按键事件的单一来源. WP8中MVVM实现的新手,是否有办法实现这一目标?

I thought I could use this everywhere across my pages as a single source for TextBox keydown events. New to MVVM implementation in WP8, curious if there's a way to achieve this?

本着MVVM的精神(尽管我不是纯粹主义者),我知道它不必专门放在视图模型中,但我仍然希望它集中化.

In the spirit of MVVM (though I'm not a purist), I understand it doesn't need to be in the viewmodel specifically, but I'd still like it centralized.

快速笔记:

  • 该类不是静态的,我知道我不能在xaml中直接使用它.
  • 我想使类成为某种StaticResource并引用xaml中的函数.但这似乎不起作用.
  • 我目前仅在代码后面使用传递函数,并将发件人传递给静态函数.

推荐答案

您需要附加行为.

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样式,并使其可在所有或许多控件中重用,而不是每次都手动添加属性.

You can also set this in an WPF Style and have it reusable within all or many controls rather than adding the property each time manually.

这篇关于我可以绑定到实用程序类吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆