【问题标题】:WPF binding not working despite correct "using final value"尽管“使用最终值”正确,WPF 绑定仍无法正常工作
【发布时间】:2017-07-14 14:41:38
【问题描述】:

我有一个绑定到 ViewModel 的属性 MinDuration 的 TextBox。并且 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");
        }
    }

所以,让 Duration = 40。现在,这是绑定的结果:

  • 如果我输入 4 -> TextBox 显示 4
  • 如果然后我输入 5(现在的值为 45)-> TextBox 显示 40(正确!)

但是,问题来了:

  • 如果我输入 4 -> TextBox 显示 4
  • if then I put 0 (value is now 40) -> TextBox 显示 40
  • 如果然后我输入例如 5(现在的值为 405)-> TextBox 显示 405(Why???

有绑定痕迹:

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 使用最终值 '40',但 TextBox 显示 405。并且绑定不适用于以 40 开头的每个数字(在此示例中)。 p>

【问题讨论】:

标签: wpf mvvm binding


【解决方案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/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多