I am currently working with data grid where I only want to allow the user to enter UP TO 20 rows of data before making CanUserAddRows to false.
I made a dependency property on my own datagrid (that derives from the original one). I tried using the event “ItemContainerGenerator.ItemsChanged” and check for row count in there and if row count = max rows, then make can user add row = false (I get an exception for that saying i’m not allow to change it during “Add Row”.
I was wondering if there is a good way on implementing this ….
Thanks and Regards,
Kevin
Here’s a subclassed
DataGridwith aMaxRowsDependency Property. The things to note about the implementation is if theDataGridis in edit mode andCanUserAddRowsis changed anInvalidOperationExceptionwill occur (like you mentioned in the question).To workaround this, a method called
IsInEditModeis called inOnItemsChangedand if it returns true we subscribe to the eventLayoutUpdatedto checkIsInEditModeagain.Also, if
MaxRowsis set to 20, it must allow 20 rows whenCanUserAddRowsis True, and 19 rows when False (to make place for theNewItemPlaceHolderwhich isn’t present on False).It can be used like this
MaxRowsDataGrid