使用InotifyDataErrorInfo使用InotifyDataErrorInfo的Silverlight验证与其光滑错误显示显示,直到我开始尝试在绑定到非字符串数据类型属性的文本框中使用它。我的计划是使用属性的定居者来做验证逻辑,从必要时从那里添加和删除错误。它适用于字符串的文本框,但如果您有一个绑定到int的文本框并且输入一个字符串,则Setter甚至没有被调用(那里我能够添加显然非数字值的错误无效的)。来自这里的建议的行动方案是什么?我正在研究估值的人,但他们太远了,在我的类中正在验证的inotifydataErrorinfo逻辑中。

假设示例:

public class MyClass
{
    private string _prod;
    public string Prod
    {
        get { return _prod; }
        set //This works great
        {
            if (value.Length > 15)
            {
                AddError("Prod", "Cannot exceed 15 characters", false);
            }
            else if (value != _prod)
            {
                RemoveError("Prod", "Cannot exceed 15 characters");
                _prod= value;
            }
        }
    }

    private int _quantity;
    public int Quantity
    {
        get { return _quantity; }
        set //if a string was entered into the textbox, this setter is not called.
        {
            int test;
            if (!int.TryParse(value, test))
            {
                AddError("Quantity", "Must be numeric", false);
            }
            else if (test != _quantity)
            {
                RemoveError("Quantity", "Must be numeric");
                _quantity= test;
            }
        }
    }

    protected Dictionary<String, List<String>> errors = 
        new Dictionary<string, List<string>>();

    public void AddError(string propertyName, string error, bool isWarning)
    {
        //adds error to errors
    }
    public void RemoveError(string propertyName, string error)
    {
        //removes error from errors
    }

    //INotifyDataErrorInfo members...
}
.

有帮助吗?

解决方案

我建议您无论如何将文本框的值绑定到字符串值,并在那里进行验证。 如果成功验证,将值传递给另一个属性,使用您实际的数据类型(例如int)。在任何其他情况下,都失败了验证。 只是一个解决方法......但适合我。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top