0

我有一个使用 ObservableCollection 绑定的数据网格。现在我想针对我的整个集合验证单元格中的重复条目。我使用 IDataError 进行验证。但是我的问题是如何在 IDataError 对象中获取集合。编辑

我的xml是:

<dg:DataGrid Name="dgPurchaseReturnEntry" 
     ItemsSource="{Binding}" 
     SelectionUnit="CellOrRowHeader"
         >
<dg:DataGrid.Columns>
   <dg:DataGridComboBoxColumn 
                   Width="300"
                   Header="Product Name"
                   SelectedValueBinding="{Binding Path=Product_Id,UpdateSourceTrigger=PropertyChanged}"                                                                                       
                   SelectedValuePath="Product_Id"
                   DisplayMemberPath="Product_Name"                                           
                   ItemsSource="{Binding Source={StaticResource ProductDataProvider}}">
        <dg:DataGridComboBoxColumn.EditingElementStyle>
            <Style TargetType="ComboBox">
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="true">
                        <Setter Property="ToolTip"
                            Value="{Binding RelativeSource={RelativeSource Self},
                            Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
                </Style.Triggers>
                <Setter Property="IsEditable" Value="True" />
            </Style>

        </dg:DataGridComboBoxColumn.EditingElementStyle>
    </dg:DataGridComboBoxColumn>
</dg:DataGrid.Columns>

我的目标是:

public class clsPurchaseBillEntryList : INotifyPropertyChanged, IDataErrorInfo
{

    private int _Product_Id;

    #region Property Getters and Setters

    public int Product_Id
    {
        get { return _Product_Id; }
        set
        {
            _Product_Id = value;

            OnPropertyChanged("Product_Id");
        }

    #endregion

    #region INotifyPropertyChanged Members

    // Declare the event
    public event PropertyChangedEventHandler PropertyChanged;

    //// Create the OnPropertyChanged method to raise the event
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    #endregion

    #region IDataErrorInfo Members

    public string Error
    {
        get
        {
            StringBuilder error = new StringBuilder();
            // iterate over all of the properties
            // of this object - aggregating any validation errors
            PropertyDescriptorCollection props = TypeDescriptor.GetProperties(this);
            foreach (PropertyDescriptor prop in props)
            {
                string propertyError = this[prop.Name];
                if (!string.IsNullOrEmpty(propertyError))
                {
                    error.Append(propertyError);
                }
            }
            return error.ToString();
        }
    }

    public string this[string name]
    {
        get
        {
            string result = null;
            if (name == "Qty")
            {

            }

            return result;
        }
    }
}

现在如何在 IDataError 的验证类中获取我的集合?

4

1 回答 1

0

遍历集合中的项目的能力不能也不应该这些对象/项目之一内完成。考虑到这一点,标准IDataErrorInfo接口无法直接帮助解决这个问题。但是,可以通过少量定制来验证项目的唯一性。

基本思想是将ExternalErrors属性添加到数据类型 bass 类或可以从类外部使用的单个数据类型类中。通过这种方式,我们可以在视图模型或任何定义了集合属性的地方执行唯一性检查,并将出现的任何错误反馈到IDataErrorInfo接口功能中。

我建议您不要再写出整个故事,而是看一下我提供的较早的答案,该答案清楚地证明了这一点。请查看我对使用 MVVM 进行正确验证问题的回答以获取更多详细信息。如果您还有其他问题,请告诉我。

于 2013-11-12T14:15:07.527 回答