WPF绑定无法正常使用#34;使用最终值"

时间:2017-07-14 14:41:39

标签: wpf mvvm binding

我有一个TextBox绑定到ViewModel的属性MinDuration。 MinDuration总是应该小于或等于ViewModel的Duration属性。所以,我的XAML:

<TextBox Text="{Binding BasePO.MinDuration, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

还有我的财产:

    private double minDuration;
    public double MinDuration
    {
        get { return minDuration; }
        set
        {
            if (value > Duration)
               minDuration = Duration;
            else
                minDuration = value;

            OnPropertyChanged("MinDuration");
        }
    }

所以,让持续时间= 40 。现在,这是绑定的结果:

  • 如果我把4 - &gt; TextBox显示4
  • 如果那时我放5(现在价值是45) - &gt; TextBox显示40(正确!)

但是,有问题:

  • 如果我把4 - &gt; TextBox显示4
  • 如果那么我把0(现在的值是40) - &gt; TextBox显示40
  • 如果那时我放了,例如,5(现在值为405) - &gt; TextBox显示405(为什么???

有一些约束力:

System.Windows.Data Warning: 90 : BindingExpression (hash=22334206): Update - got raw value '4'
System.Windows.Data Warning: 93 : BindingExpression (hash=22334206): Update - implicit converter produced '4'
System.Windows.Data Warning: 94 : BindingExpression (hash=22334206): Update - using final value '4'
System.Windows.Data Warning: 102 : BindingExpression (hash=22334206): SetValue at level 1 to PO (hash=59829654) using RuntimePropertyInfo(MinDuration): '4'
System.Windows.Data Warning: 95 : BindingExpression (hash=22334206): Got PropertyChanged event from PO (hash=59829654)
System.Windows.Data Warning: 101 : BindingExpression (hash=22334206): GetValue at level 0 from PoTableForm (hash=53931641) using RuntimePropertyInfo(BasePO): PO (hash=59829654)
System.Windows.Data Warning: 101 : BindingExpression (hash=22334206): GetValue at level 1 from PO (hash=59829654) using RuntimePropertyInfo(MinDuration): '4'
System.Windows.Data Warning: 80 : BindingExpression (hash=22334206): TransferValue - got raw value '4'
System.Windows.Data Warning: 84 : BindingExpression (hash=22334206): TransferValue - implicit converter produced '4'
System.Windows.Data Warning: 89 : BindingExpression (hash=22334206): TransferValue - using final value '4'
Поток '<Без имени>' (0x19b4) завершился с кодом 0 (0x0).


System.Windows.Data Warning: 90 : BindingExpression (hash=22334206): Update - got raw value '45'
System.Windows.Data Warning: 93 : BindingExpression (hash=22334206): Update - implicit converter produced '45'
System.Windows.Data Warning: 94 : BindingExpression (hash=22334206): Update - using final value '45'
System.Windows.Data Warning: 102 : BindingExpression (hash=22334206): SetValue at level 1 to PO (hash=59829654) using RuntimePropertyInfo(MinDuration): '45'
System.Windows.Data Warning: 95 : BindingExpression (hash=22334206): Got PropertyChanged event from PO (hash=59829654)
System.Windows.Data Warning: 101 : BindingExpression (hash=22334206): GetValue at level 0 from PoTableForm (hash=53931641) using RuntimePropertyInfo(BasePO): PO (hash=59829654)
System.Windows.Data Warning: 101 : BindingExpression (hash=22334206): GetValue at level 1 from PO (hash=59829654) using RuntimePropertyInfo(MinDuration): '40'
System.Windows.Data Warning: 80 : BindingExpression (hash=22334206): TransferValue - got raw value '40'
System.Windows.Data Warning: 84 : BindingExpression (hash=22334206): TransferValue - implicit converter produced '40'
System.Windows.Data Warning: 89 : BindingExpression (hash=22334206): TransferValue - using final value '40'


System.Windows.Data Warning: 90 : BindingExpression (hash=22334206): Update - got raw value '405'
System.Windows.Data Warning: 93 : BindingExpression (hash=22334206): Update - implicit converter produced '405'
System.Windows.Data Warning: 94 : BindingExpression (hash=22334206): Update - using final value '405'
System.Windows.Data Warning: 102 : BindingExpression (hash=22334206): SetValue at level 1 to PO (hash=59829654) using RuntimePropertyInfo(MinDuration): '405'
System.Windows.Data Warning: 95 : BindingExpression (hash=22334206): Got PropertyChanged event from PO (hash=59829654)
System.Windows.Data Warning: 101 : BindingExpression (hash=22334206): GetValue at level 0 from PoTableForm (hash=53931641) using RuntimePropertyInfo(BasePO): PO (hash=59829654)
System.Windows.Data Warning: 101 : BindingExpression (hash=22334206): GetValue at level 1 from PO (hash=59829654) using RuntimePropertyInfo(MinDuration): '40'
System.Windows.Data Warning: 80 : BindingExpression (hash=22334206): TransferValue - got raw value '40'
System.Windows.Data Warning: 84 : BindingExpression (hash=22334206): TransferValue - implicit converter produced '40'
System.Windows.Data Warning: 89 : BindingExpression (hash=22334206): TransferValue - using final value '40'

正如您所看到的,当我将405 使用最终值&#39; 40&#39; 时,但TextBox显示405.并且绑定不适用于以40开头的每个数字(在此例子)。

1 个答案:

答案 0 :(得分:1)

这实际上是一个已在.NET Framework 4.5中修复的旧错误。有关更多信息,请参阅@ Matt的答案。

Bound WPF TextBox is not updating value when the bound property enforces some business rules

如果由于某些原因无法升级到4.5,可能需要尝试此处建议的解决方法:

WPF - MVVM - Textbox getting out of sync with viewmodel property

应该提到的是,最早的官方支持的.NET Framework版本目前是4.5.2,所以毕竟升级可能是个主意:https://blogs.msdn.microsoft.com/dotnet/2015/12/09/support-ending-for-the-net-framework-4-4-5-and-4-5-1/