如何通过ValidationRules接口列表进行验证? [英] How do I pass ValidationRules an Interface List to validate against?
问题描述
我有一个文本框,我正在尝试使用验证规则来验证输入的文本以前是否尚未使用过。为此,我想向验证规则发送现有字符串的列表。
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; }
}
我知道DynamicResource不适用于'ListToCompare'。这就是我想弄清楚的。如何将列表发送到验证规则?我需要更改界面吗?
任何建议都会受到赞赏。
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.
推荐答案
不确定这会对你有所帮助,但这里有一个CodeProject文章: Windows Presentation Foundation中的验证 [ ^ ]
Not sure this will help you, but here is a CodeProject article: Validation in Windows Presentation Foundation[^]
这篇关于如何通过ValidationRules接口列表进行验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!