2017-07-14 50 views
0

我有一個文本框綁定到屬性MinDuration的ViewModel。 MinDuration始終應該小於或等於ViewModel的Duration屬性。所以,我的XAML:儘管正確的「使用最終值」WPF綁定不工作

<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 - >文本框顯示4
  • 如果當時我把5(值是45,現在) - >文本框顯示40(正確的!)

但是,存在這樣的問題:

  • 如果我把4 - >文本框顯示4
  • 如果然後我把0(該值爲40現在) - >文本框顯示40
  • 如果那麼我把,例如,5(現在的值是405) - > 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 使用最終值「40」,但文本框顯示405和結合工作不適合每個以40開頭的數字(在本例中)。

+0

您定位的是哪個版本的框架? – mm8

+0

我的.net版本是4.0 – gustav

+0

請記住投票贊成有用的答案:) https://stackoverflow.com/help/someone-answers – mm8

回答

1

這實際上是一個已在.NET Framework 4.5中修復的舊bug。有關更多信息,請參閱@ 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/