1

我的应用程序中有一个数据网格,我需要某个数据网格列的值总和等于 100。

如:

一个 20

b 20

c 60

这个是对的

一个 20

b 20

c 62

这个不对

有什么想法可以用 MVVM 广告 INotifyDataErrorInfo 来实现吗?

我现在的问题是,如果我只是将属性绑定到单元格然后抛出 ErrorChanged,datagrid 会将我锁定在我已更改数据的行上,因此除非我恢复原始值以便总和,否则我将无法移动到任何地方该列将再次变为 100。

不要在 CellEdit 上发布各种带有手动验证的代码隐藏解决方案或类似的东西——我可以自己编写这样的脏代码。我正在尝试找到适合绑定和 MVVM 的干净解决方案

据我所见,尽管Silverlight 数据网格完全损坏,未完成且没人关心。也许有人知道那里有适当的数据网格。(或者他们实际上在 Silverlight 5 中修复了它?)

现在使用以下:

在我的 ViewModel 构造函数中

        validator = new Validator(this);
        validator.AddValidationFor(() => PayElements).When(() => _payElements.Where(p1 => (!String.IsNullOrEmpty(p1.Distribution) && FormatChecker.IsDecimal(p1.Distribution, 3, 2))).Sum(p2 => Decimal.Parse(p2.Distribution)) != 100).Show("ERROR!");

PayElements 是我的集合(顺便说一句,我的错,它实际上是从 _payElements 创建的 ICollectionView,即 ObservableCollection)。然后对任何对象进行 OnChanges

        validator.ValidateAll();

这会导致在构造函数中创建的验证规则被评估。在此之后,我的 viewmodel 的整个对象集合的 errorCOllection 中有错误,但它没有显示任何红色边框。在列表中我可以看到有错误并禁用保存按钮:)

4

1 回答 1

0

我已经在数据网格上实现了令人讨厌的验证,除非您必须执行异步验证,否则它可以从绑定对象中很好地完成。我不确定您绑定到列表的对象,但我会使用以下内容:

public string Col1Wrapper
{
    get
    {
        return this.Col1;
    }
    set
    {
        ValidateRequired("Col1Wrapper", value, "Required");
        ValidateRegularExpression("Col1Wrapper", value, @"^[\d]+$", "Must be digit");
        ValidateTotal(value,Col2,Col3,total);//in your case
        this.Col1 = value;
        this.RaisePropertyChanged("Col1Wrapper");
    }
}

public string Col2Wrapper
{
    get
    {
        return this.Col2;
    }
    set
    {
        ValidateRequired("Col2Wrapper", value, "Required");
        ValidateRegularExpression("Col2Wrapper", value, @"^[\d]+$", "Must be digit");
        ValidateTotal(Col1,value,Col3,total);//in your case
        this.Col2 = value;
        this.RaisePropertyChanged("Col2Wrapper");
    }
}

public string Col3Wrapper
{
    get
    {
        return this.Col3;
    }
    set
    {
        ValidateRequired("Col3Wrapper", value, "Required");
        ValidateRegularExpression("Col3Wrapper", value, @"^[\d]+$", "Must be digit");
        ValidateTotal(Col1,Col2,value,total);//in your case
        this.Col3 = value;
        this.RaisePropertyChanged("Col3Wrapper");
    }
}

然后使用http://msdn.microsoft.com/en-us/library/system.componentmodel.inotifydataerrorinfo%28VS.95%29.aspx

默认实现在示例部分。

public bool ValidateRequired(string property, string value, string errorMessage)
{
    bool isValid = true;

    if (value == null || value == string.Empty)
    {
        AddError(property, errorMessage, false);
        isValid = false;
    }
    else RemoveError(property, errorMessage);
    RaiseErrorsChanged(property);
    return isValid;
}

public bool ValidateRegularExpression(string property, string value, string expression, string errorMessage)
{
    ...
}

public bool ValidateTotal(string property,int 1, int 2, int 3, int total, string errorMessage)
{
    if((1+2+3) != total)
        AddError(property, errorMessage, false);
    else RemoveError(property, errorMessage);
}

添加错误方法将触发验证摘要显示在数据网格上。希望这就是你要找的。

于 2012-06-06T13:12:30.423 回答