我有一个使用ObservableCollection绑定的数据网格。如果用户在单元格上创建了一个重复的条目,那么我想对整个集合进行验证,例如在我的数据网格中,我将一个DatagridComboboxColumn用于选择Items,如果用户然后,选择一个已经存在于datagrid上的项目,我想给一些视觉反馈带有消息'所选项已经存在'如何实现这一目标? 我的xaml是:
<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>
</dg:DataGrid>
我的目标是:
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;
}
}
}
}
如何捕获datagrid上的重复条目?