1. Value. and 2. HasValidationError." />
开发者

Can we set property of source object, on validation?

开发者 https://www.devze.com 2023-02-09 02:21 出处:网络
I have a wpf-mvvm application. In the below code, \"PartBPremiumBuydown\" is an instance of a class. which has two properties => 1. Value. and 2. HasValidationError.

I have a wpf-mvvm application.

In the below code, "PartBPremiumBuydown" is an instance of a class. which has two properties => 1. Value. and 2. HasValidationError.

Property "Value" is used for binding to textbox. If there is any validation error...Can I set HasValidationError=true ?

  <TextBo开发者_StackOverflow中文版x  ToolTip="{Binding RelativeSource={RelativeSource Self}, 
                      Path=(Validation.Errors).CurrentItem.ErrorContent}">
                        <TextBox.Text>
                            <Binding Path="PartBPremiumBuydown.Value" 
                                      ValidatesOnDataErrors="True"
                                      UpdateSourceTrigger="PropertyChanged"
                             Converter="{x:Static localns:Converters.DecimalToCurrency}">
                                <Binding.ValidationRules>
                                    <localns:CurrencyRule />
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>
                    </TextBox>


You should have PartBPremiumBuydown implement the IDataErrorInfo interface, similar to the below code:

public string Error { get; private set; }
public string this[string propertyName]
{
    get
    {
        string mError = string.Empty;
        if (propertyName == "Value" 
            && !<insert your rule>)
        {
            mError = "Validation error text."
        }
        Error = mError;
        return (string.IsNullOrWhiteSpace(mError))// if   NOTHING 
            ? null                                // then return null
            : mError;                             // else return error
    }
}

Now, when you bind your TextBox to Value, if the user enters in text which breaks your rule, the validation error will show on the TextBox.

0

精彩评论

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

关注公众号