Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7741683
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T09:05:16+00:00 2026-06-01T09:05:16+00:00

I have a DataGrid in a WPF application which has for its ItemsSource a

  • 0

I have a DataGrid in a WPF application which has for its ItemsSource a custom collection that I wrote. The collection enforces that all its items satisfy a certain requirement (namely they must be between some minimum and maximum values).

The collection’s class signature is:

   public class CheckedObservableCollection<T> : IList<T>, ICollection<T>, IList, ICollection,
                                            INotifyCollectionChanged
                                             where T : IComparable<T>, IEditableObject, ICloneable, INotifyPropertyChanged

I want to be able to use the DataGrid feature in which committing an edit on the last row in the DataGrid results in a new item being added to the end of the ItemsSource.

Unfortunately the DataGrid simply adds a new item created using the default constructor. So, when adding a new item, DataGrid indirectly (through its ItemCollection which is a sealed class) declares:

ItemsSource.Add(new T())

where T is the type of elements in the CheckedObservableCollection. I would like for the grid to instead add a different T, one that satisfies the constraints imposed on the collection.

My questions are: Is there a built in way to do this? Has somebody done this already? What’s the best (easiest, fastest to code; performance is not an issue) way to do this?

Currently I just derived DataGrid to override the OnExecutedBeginEdit function with my own as follows:

public class CheckedDataGrid<T> : DataGrid where T : IEditableObject, IComparable<T>, INotifyPropertyChanged, ICloneable
{
  public CheckedDataGrid() : base() { }

  private IEditableCollectionView EditableItems {
     get { return (IEditableCollectionView)Items; }
  }

  protected override void OnExecutedBeginEdit(ExecutedRoutedEventArgs e) {
     try {
        base.OnExecutedBeginEdit(e);
     } catch (ArgumentException) {
        var source = ItemsSource as CheckedObservableCollection<T>;
        source.Add((T)source.MinValue.Clone());
        this.Focus();
     }
  }
}

Where MinValue is the smallest allowable item in the collection.

I do not like this solution. If any of you have advice I would be very appreciative!

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T09:05:18+00:00Added an answer on June 1, 2026 at 9:05 am

    For anybody interested, I ended up solving the problem by just deriving from BindingList<T> instead of ObservableCollection<T>, using my derived class as the ItemsSource in a regular DataGrid:

       public class CheckedBindingList<T> : BindingList<T>, INotifyPropertyChanged where T : IEditableObject, INotifyPropertyChanged
    {
      public event PropertyChangedEventHandler PropertyChanged;
    
      private Predicate<T> _check;
      private DefaultProvider<T> _defaultProvider;
    
      public CheckedBindingList(Predicate<T> check, DefaultProvider<T> defaultProvider) {
         if (check == null)
            throw new ArgumentNullException("check cannot be null");
         if (defaultProvider != null && !check(defaultProvider()))
            throw new ArgumentException("defaultProvider does not pass the check");
    
         _check = check;
         _defaultProvider = defaultProvider;
      }
    
      /// <summary>
      /// Predicate the check item in the list against.
      /// All items in the list must satisfy Check(item) == true
      /// </summary>
      public Predicate<T> Check {
         get { return _check; }
    
         set {
            if (value != _check) {
               RaiseListChangedEvents = false;
    
               int i = 0;
               while (i < Items.Count)
                  if (!value(Items[i]))
                     ++i;
                  else
                     RemoveAt(i);
    
               RaiseListChangedEvents = true;
               SetProperty(ref _check, value, "Check");
    
               ResetBindings();
            }
         }
      }
    
      public DefaultProvider<T> DefaultProvider {
         get { return _defaultProvider; }
         set {
            if (!_check(value()))
               throw new ArgumentException("value does not pass the check");
         }
      }
    
      protected override void OnAddingNew(AddingNewEventArgs e) {
         if (e.NewObject != null)
            if (!_check((T)e.NewObject)) {
               if (_defaultProvider != null)
                  e.NewObject = _defaultProvider();
               else
                  e.NewObject = default(T);
            }
    
         base.OnAddingNew(e);
      }
    
      protected override void OnListChanged(ListChangedEventArgs e) {
         switch (e.ListChangedType) {
            case (ListChangedType.ItemAdded):
               if (!_check(Items[e.NewIndex])) {
                  RaiseListChangedEvents = false;
                  RemoveItem(e.NewIndex);
                  if (_defaultProvider != null)
                     InsertItem(e.NewIndex, _defaultProvider());
                  else
                     InsertItem(e.NewIndex, default(T));
                  RaiseListChangedEvents = true;
               }
               break;
            case (ListChangedType.ItemChanged):
               if (e.NewIndex >= 0 && e.NewIndex < Items.Count) {
                  if (!_check(Items[e.NewIndex])) {
                     Items[e.NewIndex].CancelEdit();
                     throw new ArgumentException("item did not pass the check");
                  }
               }
               break;
            default:
               break;
         }
    
         base.OnListChanged(e);
      }
    
      protected void SetProperty<K>(ref K field, K value, string name) {
         if (!EqualityComparer<K>.Default.Equals(field, value)) {
            field = value;
            if (PropertyChanged != null)
               PropertyChanged(this, new PropertyChangedEventArgs(name));
         }
      }
    }
    

    This class is incomplete, but the implementation above is enough for validating lists of statically-typed (not built by reflection or with the DLR) objects or value types.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF application which shows items in a DataGrid (XCeed DataGrid). The
I have a WPf Toolkit-datagrid..in my application which follows MVVM pattern. How I can
I am making a WPF-application.I have a datagrid with a column header that contains
I have a WPF application with a datagrid who's ItemsSource is a BindingList. When
I have a WPF application with two DataGrids that share the same ItemsSource. When
I have a WPF (C# and .NET 4) application that has a long running
I'm writing WPF application, that's using MVVMLight. I have a DataGrid and I wanna
Goal I have a WPF application that contain text inside a DataGrid . I
I have a problem with my WPF application. I have a datagrid (Wpf Toolkit),
I have a DataGrid, with an ItemTemplate that has an image and label. In

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.