结束编辑时Silverlight Datagrid验证

时间:2011-11-11 12:33:40

标签: validation silverlight-4.0 datagrid

我需要对网格列进行验证。该列中的所有单元格都是空白的。用户需要输入数据,然后我需要验证它。

我目前对单元格的验证工作,但它适用于Begin Edit事件而不是结束编辑事件。这是没有意义的,因为验证需要检查他们是否输入了信息。

如何在结束编辑事件中验证验证?

提前感谢您的帮助

尼文。

代码:

    private string _reference;
    public string Reference
    {
        get 
        {
            return _reference; 
        }
        set 
        {
            _reference = value;
            ChangeValue("Reference");
        }
    }


        public event PropertyChangedEventHandler PropertyChanged;

        public void ChangeValue(string propName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propName));
            }
        }
        public string this[string columnName]
        {
            get
            {
                string msg = null;
                if (columnName == "Reference")
                {
                    if (Reference == "" &&  ValidateItem)
                    {
                        msg = "Reference is Required. Please Enter a Reference";
                    }
                }
                return msg;
            }
        }

        string err;
        public string Error
        {
            get { return err; }
        }

1 个答案:

答案 0 :(得分:0)

我刚用下面的自定义代码解决了这个问题。我没有时间测试所有场景,但我对结果非常有信心。

public class ExtendedDataGrid : DataGrid {
    private ValidationSummary _validationSummary = null;
    private bool _validationFilterBehaviorApplied = false;
    private bool _validationFilterBehaviorRestored = false;

    public ExtendedDataGrid() { }

    public override void OnApplyTemplate() {
        base.OnApplyTemplate();

        this._validationSummary = (ValidationSummary)this.GetTemplateChild("ValidationSummary");
    }

    protected override void OnBeginningEdit(DataGridBeginningEditEventArgs e) {
        if (!_validationFilterBehaviorApplied) { 
            _validationSummary.Filter = ValidationSummaryFilters.None;
            _validationFilterBehaviorApplied = true;
        }

        base.OnBeginningEdit(e);
    }

    protected override void OnRowEditEnding(DataGridRowEditEndingEventArgs e) {
        if (_validationFilterBehaviorApplied && !_validationFilterBehaviorRestored) {
            _validationSummary.Filter = ValidationSummaryFilters.All;
            _validationFilterBehaviorRestored = true;
        }

        base.OnRowEditEnding(e);
    }

    protected override void OnRowEditEnded(DataGridRowEditEndedEventArgs e) {
        _validationFilterBehaviorApplied = false;
        _validationFilterBehaviorRestored = false;

        base.OnRowEditEnded(e);
    }
}