I have a Silverlight 3 tool library with a DataGrid user control. This grid does not have direct access to WCF RIA Services object types, so I use reflection to add a new element when the user clicks on the grid when it is empty:
private void InsertEmptyRecord() { if (this._dataGrid.ItemsSource == null) return; Type[] typeParameters = this._dataGrid.ItemsSource.GetType().GetGenericArguments(); if (typeParameters.Count() > 0) { Type itemType = typeParameters[0]; object newItem = System.Activator.CreateInstance(itemType); Type sourceType = typeof(System.Windows.Ria.EntityCollection<>); Type genericType = sourceType.MakeGenericType(itemType); System.Reflection.MethodInfo addMethod = genericType.GetMethod("Add"); addMethod.Invoke(this._dataGrid.ItemsSource, new object[] { newItem });
This works, but I need to check it also after adding a new item. There are two ways to do this:
- Make the user enter edit mode for the first cell of a new row in the grid. (This will force a check if they are still on the page.)
- Power checks to start immediately when a new line (or when the grid loses focus.)
I was unable to get any of them to work. Tried this, but it selects only the line, does not force to perform checks:
this._dataGrid.SelectedItem = newItem; System.ComponentModel.IEditableObject editableItem = newItem as System.ComponentModel.IEditableObject; if (editableItem != null) editableItem.BeginEdit();
Any suggestions?
Nick gotch
source share