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標籤
它不應該不管有多少次火災。造成什麼問題? – Bryant
實際上WPF驗證框架並不「保證」整個「依賴」框架的單一驗證通行證。嘗試提高您的驗證代碼速度。 –
@Bryant - 這是在WP7上(只是添加了標籤),我不知道如何向用戶展示錯誤。爲了看到一個錯誤,我在上面的文本框中添加了一個事件處理函數'BindingValidationError'。這會彈出一個消息框,顯示IDataErrorInfo返回的錯誤的內容。也許這是一個不正確的實現。 –