2011-09-26 79 views
0

我遇到了IDataErrorInfo多次被解雇的问题。为什么IDataErrorInfo多次触发?

Transaction Class

public class Transaction : INotifyPropertyChanged, INotifyPropertyChanging, IDataErrorInfo 
{ 

private Double? _transAmount; 

[Column(DbType = "decimal(19,4)")] 
public Double? TransAmount 
{ 
    get { return _transAmount; } 
    set 
    { 
     if (_transAmount != value) 
     { 
      NotifyPropertyChanging("TransAmount"); 
      _transAmount = value; 
      NotifyPropertyChanged("TransAmount"); 
     } 
    } 
} 

#region INotifyPropertyChanged Members 

public event PropertyChangedEventHandler PropertyChanged; 

// Used to notify that a property changed 
private void NotifyPropertyChanged(string propertyName) 
{ 
    if (PropertyChanged != null) 
    { 
     PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

#endregion 

#region INotifyPropertyChanging Members 

public event PropertyChangingEventHandler PropertyChanging; 

// Used to notify that a property is about to change 
private void NotifyPropertyChanging(string propertyName) 
{ 
    if (PropertyChanging != null) 
    { 
     PropertyChanging(this, new PropertyChangingEventArgs(propertyName)); 
    } 
} 

#endregion 

#region Data Validation 

public string Error { get { return null; } } 

public string this[string property] 
{ 
    get 
    { 
     switch (property) 
     { 
      case "TransAmount": 
       if (TransAmount != null) 
       { 
        double value; 
        bool valid = double.TryParse(TransAmount.ToString(), out value); 

        if (!valid) { return TransAmount.ToString() + " is not a valid number"; } 
        else if (value <= 0) { return "Dollar amount must be greater than $0.00"; } 
       } 
       return null; 
      default: 
       return null; 
     } 
    } 
} 

#endregion 

} 

和XAML

<toolkit:PhoneTextBox Grid.Row="1" Grid.Column="1" HorizontalAlignment="Stretch" 
         x:Name="txtAmount" Width="Auto" 
         Text="{Binding TransAmount, Mode=TwoWay, NotifyOnValidationError=True, StringFormat=\{0:c\}, ValidatesOnDataErrors=True, ValidatesOnExceptions=True}" 
         BindingValidationError="txtAmount_BindingValidationError" InputScope="CurrencyAmount" 
         GotFocus="txtAmount_GotFocus" 
         LostFocus="txtAmount_LostFocus"> 
</toolkit:PhoneTextBox> 

我不知道的格局,但验证方法被击中的2-3倍?为什么?

编辑1 正在txtAmount_LostFocus事件中设置值TransAmount。

编辑2 加入WP7标签

+0

它不应该不管有多少次火灾。造成什么问题? – Bryant

+0

实际上WPF验证框架并不“保证”整个“依赖”框架的单一验证通行证。尝试提高您的验证代码速度。 –

+0

@Bryant - 这是在WP7上(只是添加了标签),我不知道如何向用户展示错误。为了看到一个错误,我在上面的文本框中添加了一个事件处理函数'BindingValidationError'。这会弹出一个消息框,显示IDataErrorInfo返回的错误的内容。也许这是一个不正确的实现。 –

回答

0

诀窍如下。

使用DataErrorValidationRule代替ValidatesOnDataErrors = True。

<TextBox> 
    <TextBox.Text> 
     <Binding Path="..." UpdateSourceTrigger="LostFocus" NotifyOnValidationError="True"> 
       <Binding.ValidationRules> 
        <DataErrorValidationRule ValidatesOnTargetUpdated="False"/> 
       </Binding.ValidationRules> 
      </Binding> 
    </TextBox.Text> 
</TextBox> 

请参阅本文https://social.msdn.microsoft.com/forums/vstudio/en-US/099164f8-72aa-4c59-a7b6-7ccbd56702ce/idataerrorinfo-validation-called-twice

相关问题