我有一个使用 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 的验证类中获取我的集合?