开发者

Applying a validation rule on a binding to display a validation result only on the UI

I am applying a validation rule to the binding on a text box. I have got the validation right from the UI perspective in that I see the error message on the tool tip and have the error template applied too(Jus开发者_开发知识库t the usual red border).

However, the validation that I have to display is not super critical and is sufficient to just be displayed on the UI. The problem that I have with the binding is that the validation rule prevents updates on the source object once a validation rule gets violated I want the source to get updated with exactly the content of the textbox.

Is there a way to display the error template on the UI without affecting the bound source.

My code looks something like

<TextBox Name="texBox">
      <TextBox.Text>
        <Binding Path="ProductCode" UpdateSourceTrigger="PropertyChanged">
          <Binding.ValidationRules>
            <jas:RegexValidationRule
              RegexText="^[A-Z]{3}\.[0-9]{3}$"
              ErrorMessage="Invalid product code.  (Examples: ABC.123  xyz.789)"
              RegexOptions="IgnoreCase"
              />
          </Binding.ValidationRules>
        </Binding>
      </TextBox.Text>


This happens because if a validation error or other type of error occurs at any time during the binding process, the process is halted.I guess you have to set the ValidationStep property to UpdatedValue

Sample:

 <jas:RegexValidationRule ValidationStep="UpdatedValue"
              RegexText="^[A-Z]{3}\.[0-9]{3}$"
              ErrorMessage="Invalid product code.  (Examples: ABC.123  xyz.789)"
              RegexOptions="IgnoreCase"
              />

Please check the "Validation Process" section in Data Binding Overview.This will give you good overview of what you are tying to do


You could try looking into IDataErrorInfo instead. Then you'll get the validation in your backing class (ViewModel) so the Text in the displayed TextBox will be in sync with the backing property. In your case it will look something like this

<TextBox Name="texBox"> 
    <TextBox.Text> 
        <Binding Path="ProductCode" UpdateSourceTrigger="PropertyChanged"/>
    </TextBox.Text>
</TextBox>

In the datacontext backing class

public class YourClass : IDataErrorInfo 
{
    //...

    #region IDataErrorInfo Members
    public string Error
    {
        get { throw new NotImplementedException(); }
    }

    public string this[string columnName]
    {
        get
        {
            string result = null;
            if (columnName == "ProductCode")
            {
                // Do your Regex Validation.
                if (regexValidationFailed)
                {
                    result = "Validation Error Text/Tooltip";
                }
            }
            if (columnName == "SomeOtherProperty)
            //...

            return result;
        }            
    }
    #endregion
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜