2011-09-26 3 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 값 TransAmount가 txtAmount_LostFocus 이벤트에서 설정됩니다.

편집 2 추가 WP7 태그

+0

를 참조하십시오. 그게 무슨 문제입니까? – Bryant

+0

실제로 WPF 유효성 검사 프레임 워크는 전체 "종속성"프레임 워크의 단일 유효성 검사 통과를 "보장"하지 않습니다. 신속하게 검증 코드를 개선하십시오. –

+0

@Bryant - WP7 (태그가 추가됨)에 있으며 사용자에게 오류를 표시하는 방법을 잘 모르겠습니다. 오류를보기 위해 위의 텍스트 상자에 이벤트 처리기 인 BindingValidationError를 추가했습니다. 그러면 IDataErrorInfo에서 반환 한 오류의 내용을 표시하는 메시지 상자가 나타납니다. 아마도 이것은 잘못된 구현입니다. –

답변

0

비결은 다음과 같다.

ValidatesOnDataErrors = True 대신 DataErrorValidationRule을 사용하십시오.

<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

관련 문제