2

我有一个使用 ObservableCollection 绑定的数据网格。如果用户在单元格上创建了​​重复条目,那么我想针对整个集合进行验证,例如在我的数据网格中,如果用户选择一个项目,我放置一个 DatagridComboboxColumn 来选择项目那么数据网格上已经存在,我想用消息'所选项目已经存在'来放置一些视觉反馈如何做到这一点?我的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>
</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;
        }
    }

    }
}

如何捕获数据网格上的重复条目?

4

0 回答 0