Click here to Skip to main content
15,892,537 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a textbox and I am trying to use validation rules to verify that the text entered is has not already been used before. To do so I want to send the Validation rule a list of the existing strings.

<TextBox Name="txtModuleName" Grid.Column="1" Margin="5">
    <TextBox.Text>
        <Binding FallbackValue="59" RelativeSource="{RelativeSource Self}" Path="Text">
            <Binding.ValidationRules>
                <val:StringRangeValidationRule
                    MinimumLength="1"
                    ErrorMessage="Module Name is required."/>
                <val:StringNotInListValidationRule
                    ErrorMessage="Module Name already Exists."
                    ListToCompare="{DynamicResource list}"/>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>


This is my rather simple Validation rule I am trying to use.

public class StringNotInListValidationRule : ValidationRule
   {
       public List<ISearch> ListToCompare { get; set; }

       public string ErrorMessage { get; set; }

       public override ValidationResult Validate(object value, CultureInfo cultureInfo)
       {
           ValidationResult result = new ValidationResult(true, null);
           string inputString = (value ?? string.Empty).ToString();
           if (null == ListToCompare || ListToCompare.Exists(x => x.Name == inputString))
           {
               result = new ValidationResult(false, this.ErrorMessage);
           }

           return result;
       }
   }

   public interface ISearch
   {
       string Name { get; set; }
   }


I know DynamicResource doesn't work for the 'ListToCompare'. That is what I am trying to figure out. How can I send the list to the validation rule? Do I need to change the Interface?

Any suggestions would be appreciated.
Posted
Updated 2-Aug-13 9:01am
v2

1 solution

Not sure this will help you, but here is a CodeProject article: Validation in Windows Presentation Foundation[^]
 
Share this answer
 
Comments
bowlturner 5-Aug-13 8:53am    
Thanks, but that is where I got a bit of my code I'm currently using. So, no, my answer isn't in there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900