开发者

Why is IDataErrorInfo is firing multiple times?

开发者 https://www.devze.com 2023-04-07 18:54 出处:网络
I am having an issue where IDataErrorInfo is getting fired multiple times. Transaction Class public class Transaction : INotifyPropertyChanged, INotifyPropertyChanging, IDataErrorInfo

I am having an issue where IDataErrorInfo is getting fired multiple times.

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

} 

and the 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>

I'm not sure of the pattern, but validation method is getting hit 2-3 times? Why?

Edit 1 The value TransAmount is being set in the txtAmount_LostFocus event.

Edit 2 Added WP7 tag


The trick is the following.

Use DataErrorValidationRule instead of ValidatesOnDataErrors=True.

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

Refer to this article https://social.msdn.microsoft.com/forums/vstudio/en-US/099164f8-72aa-4c59-a7b6-7ccbd56702ce/idataerrorinfo-validation-called-twice

0

精彩评论

暂无评论...
验证码 换一张
取 消