UpdateSourceTrigger =的PropertyChanged器和转换器 [英] UpdateSourceTrigger=PropertyChanged and Converter

查看:151
本文介绍了UpdateSourceTrigger =的PropertyChanged器和转换器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的转换,增加了一个+符号的输入在文本框。当号码被输入我要启动一些行动,但我不想等到文本框失去焦点:我想更新在用户输入立即绑定文本。

I have a simple Converter that adds a "+" symbol to a positive number that is entered in a TextBox. When the number is entered I want to initiate some action, but I don't want to wait until the TextBox loses focus: I want to update the binding immediately as the user enters the text.

文本框的默认行为是,当用户从盒子导航离开,绑定源更新(UpdateSourceTrigger =引发LostFocus)。在这种情况下,我的转换器工作正常,而+添加。然而,当我将其更改为以下,+是的永远的补充。

The default behaviour of a TextBox is that when a user navigates away from the box, the binding source is updated (UpdateSourceTrigger=LostFocus). In this scenario, my converter works as expected, and the + is added. However, when I change it to the following, the + is never added.

<TextBox Text="{Binding Value, Converter={StaticResource PlusConverter}, UpdateSourceTrigger=PropertyChanged}" />

我可以想像,有一个很好的理由,为什么我converter.Convert()方法将不会调用。我想有以下行为:

I can imagine that there is a good reason why my converter.Convert() method is not called. I would like to have the following behaviour:

  1. 当用户输入文本时,源会立即更新
  2. 文本框失去焦点,+加。
  1. When the users enters text, the source is updated immediately
  2. When the TextBox loses focus, the + is added.

现在我正在寻找这样做的不错,尤其是通用的方法(因为我有很多这样的文本框,在我的应用程序)。 到目前为止,我还没有能够拿出一个妥善的解决办法。

Now I'm looking for a nice, but especially GENERIC way of doing this (as I have a lot of these TextBoxes in my app). So far I haven't been able to come up with a proper solution.

推荐答案

谢谢您的回答!我看着这个问题我自己有点futher,并提出了以下解决方案(这我并不完全满意,但它工作正常)

Thanks for your answers! I looked into this issue myself a bit futher and came up with the following solution (which I'm not entirely satisfied with, but it works fine)

我创建了一个CustomControl的将功能添加到文本框。

I've created a CustomControl that adds functionality to the TextBox.

它的LostFocus事件提供事件处理程序 当这个事件发生时,我的转换器调用。

It provided an event handler for the LostFocus event When this event occurs, my converter is called.

不过,我解决了转换器的方式也不是很令人满意的(我把它从一个与我的文本框相关联的风格)。样式有一个二传手的Text属性。从这个二传手,我可以访问我的转换器。

However, the way I resolve the Converter is not very satisfying (I take it from the Style that is associated with my TextBox). The Style has a Setter for the Text property. From that setter I can access my Converter.

当然,我也可以做一个PlusTextBox,我却用不同的转换器和我想要一个通用的解决方案。

Of course I could also make a "PlusTextBox", but I use different converters and I wanted a generic solution.

public class TextBoxEx : TextBox
{
    public TextBoxEx()
    {
        AddHandler(LostFocusEvent,
          new RoutedEventHandler(CallConverter), true);
    }

    public Type SourceType
    {
        get { return (Type)GetValue(SourceTypeProperty); }
        set { SetValue(SourceTypeProperty, value); }
    }

    // Using a DependencyProperty as the backing store for SourceType.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty SourceTypeProperty =
        DependencyProperty.Register("SourceType", typeof(Type), typeof(TextBoxEx), new UIPropertyMetadata(null));

    private static void CallConverter(object sender, RoutedEventArgs e)
    {
        TextBoxEx textBoxEx = sender as TextBoxEx;
        if (textBoxEx.Style == null) {
            return;
        }
        if (textBoxEx.SourceType == null) {
        }
        foreach (Setter setter in textBoxEx.Style.Setters) {
            if (setter.Property.ToString() == "Text") {
                if (! (setter.Value is Binding) ) {
                    return;
                }
                Binding binding = setter.Value as Binding;
                if (binding.Converter == null) {
                    return;
                }
                object value = binding.Converter.ConvertBack(textBoxEx.Text, textBoxEx.SourceType, binding.ConverterParameter, System.Globalization.CultureInfo.CurrentCulture);
                value = binding.Converter.Convert(value, typeof(string), binding.ConverterParameter, System.Globalization.CultureInfo.CurrentCulture);
                if (!(value is string)) {
                    return;
                }
                textBoxEx.Text = (string)value;
            }
        }
    }
}

这篇关于UpdateSourceTrigger =的PropertyChanged器和转换器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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