rem*_*rem 26 validation wpf xaml namespaces
我试图在WPF中使用带有验证规则的数据绑定控件的验证输入.在wpf窗口的代码隐藏文件中,我有一个类:
public class posintValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
{
string _strInt = value.ToString();
int _int = -1;
if (!Int32.TryParse(_strInt, out _int))
return new ValidationResult(false, "Value must be an integer");
if (_int < 0)
return new ValidationResult(false, "Value must be positive");
return new ValidationResult(true, null);
}
}
Run Code Online (Sandbox Code Playgroud)
在XAML中还有一个样式错误模板.
当我在XAML中放入带验证的文本框时:
<TextBox.Text>
<Binding Path="seconds" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:posintValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
Run Code Online (Sandbox Code Playgroud)
我得到一个编译时错误: ''local'是一个未声明的命名空间.XML无效.
我应该如何local:posintValidationRule
在我的XAML中声明?
Str*_*ter 51
在XAML文件的顶部,您需要声明"本地"命名空间是什么; 以及默认的Microsoft XAML内容.像这样的东西:
xmlns:local="clr-namespace:YourApplication"
Run Code Online (Sandbox Code Playgroud)
请注意,这假定在"YourApplication"中的根命名空间中定义了"posintValidationRule".