物业设置者的强制价值 - Silverlight 5 [英] Coerce Value in Property Setter - Silverlight 5

查看:126
本文介绍了物业设置者的强制价值 - Silverlight 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的示例中,我们已将一个ViewModel绑定到具有名为Message的单个属性的视图。此属性绑定到具有双向绑定的TextBox。对于这个测试,我们在设置器中做了一些有价值的强制,并提高了属性。

In the example below, we have bound a ViewModel to a view with a single property called Message. This property is bound to a TextBox with a two way binding. For this test, we're doing some value coercion in the setter and raising the property changed again.

在Silverlight 4中,这样做完美。如果在属性设置器中更改了消息属性,则文本框将看到新值。例如。在文本框中输入A并失去焦点将导致Aaaaaaaaaaa出现,因为值已更改。

In Silverlight 4, this worked perfectly. If the message property changed in the property setter, the textbox would see the new value. E.g. typing "A" into the textbox and losing focus would result in Aaaaaaaaaaa appearing as the value was changed.

然而,在Silverlight 5中,这似乎被破坏/更改。在setter中修改值之后,吸气剂不会被击中。在之间添加一个IValueConverter,显示Convert / ConvertBack方法永远不会被打。似乎在第4版和第5版之间的一些根本性发生了变化。有没有改变?这是一个错误吗?

In Silverlight 5 however, this seems to be broken/changed. The getter is never hit after the value is modified in the setter. Adding a IValueConverter in between, shows that the Convert/ConvertBack methods are never hit. It seems that something fundamental has changed between version 4 and 5. Has there been any changes? Is this a bug?

public class ViewModel : INotifyPropertyChanged
{
    private string _message;

    public event PropertyChangedEventHandler PropertyChanged;

    public string Message
    {
        get
        {
            return _message; 
        }
        set
        {
            _message = value;
            this.RaisePropertyChanged();

            if (_message.Length < 10)
            {
                _message = _message.PadRight(10, 'a');
                this.RaisePropertyChanged();
            }
        }
    }

    private void RaisePropertyChanged()
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs("Message"));
        }
    }
}


推荐答案

这已被Microsoft确认为Silverlight 5中的错误。它只发生在调试模式,所以对于释放模式是很好的。

This has been confirmed as a bug in Silverlight 5 by Microsoft. It only occurs in debug mode, so is fine for release mode.

如果您禁用XAML绑定调试,则问题消失。

If you disable XAML binding debugging, the issue is gone.

Binding.IsDebuggingEnabled = false; 

这篇关于物业设置者的强制价值 - Silverlight 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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