如何在 WPF XAML 中声明命名空间? [英] How to declare a namespace in WPF XAML?
问题描述
我试图在 WPF 中使用带有验证规则的数据绑定控件的验证输入.在 wpf 窗口的代码隐藏文件中,我有一个类:
I am trying to use in WPF a validating input of databound controls with validation rules. In the code behind file of a wpf window I have a class:
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);
}
}
在 XAML 中还有一个样式错误模板.
In XAML there is also a style error template.
当我在 XAML 中放置带有验证的文本框时:
When I put a textbox with validation in XAML:
<TextBox.Text>
<Binding Path="seconds" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<local:posintValidationRule/>
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
我收到一个编译时错误:''local' 是一个未声明的命名空间.'XML 无效.
I get a compile time error: ''local' is an undeclared namespace.' XML is not valid.
我应该如何在 XAML 中声明 local:posintValidationRule
?
How I should declare local:posintValidationRule
in my XAML?
推荐答案
在 XAML 文件的顶部,您需要声明您的本地"命名空间是什么;与默认的 Microsoft XAML 内容一起.像这样:
At the top of your XAML file, you need to declare what your "local" namespace is; alongside the default Microsoft XAML stuff. Something like this:
xmlns:local="clr-namespace:YourApplication"
请注意,这里假设在YourApplication"的根命名空间中定义了posintValidationRule".
Note this assumes that "posintValidationRule" is defined at the root namespace in "YourApplication".
这篇关于如何在 WPF XAML 中声明命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!