本文介绍了如何在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>

我收到一个编译时错误: ``本地''是未声明的名称空间. 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中声明名称空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 04:17