如何立即验证 Silverlight 3 Datagrid 中新插入的行? [英] How do I Immediately Validate a Newly Inserted Row in a Silverlight 3 Datagrid?
问题描述
我有一个带有自定义 DataGrid 用户控件的 Silverlight 3 工具库.此网格无法直接访问 WCF RIA 服务实体类型,因此当用户在网格为空时单击该网格时,我使用反射来添加新项目:
I have a Silverlight 3 tools library with a custom DataGrid user control. This grid has no direct access to the WCF RIA Services entity types so I'm using reflection to add a new item when the user clicks on the grid when it's 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 });
// == Validate data here ==
}
}
这有效,但我还需要在添加新项目后对其进行验证.我可以看到有两种方法可以做到这一点:
This works, but I need it to also validate after the new item is added. There are two ways I can see to do this:
- 强制用户进入编辑模式对于新行的第一个单元格网格.(这将迫使如果他们点击任何地方进行验证其他在页面上.)
- 强制验证当新行出现时立即运行添加(或当网格松动时重点.)
我无法让其中任何一个工作.试过这个,但它只选择行,不强制验证运行:
I haven't been able to get either of these to work. Tried this but it only selects the row, doesn't force the validations to run:
this._dataGrid.SelectedItem = newItem;
System.ComponentModel.IEditableObject editableItem = newItem as System.ComponentModel.IEditableObject;
if (editableItem != null)
editableItem.BeginEdit();
有什么建议吗?
推荐答案
在 这个问题.
我在上面代码的== Validate data here =="部分添加了以下内容:
I added the following to the "== Validate data here ==" section in the code from above:
DataGridRow newRow = this._dataGrid.ChildrenOfType<DataGridRow>().FirstOrDefault();
if (newRow != null)
{
newRow.Loaded += (sender, e) =>
{
this._dataGrid.CurrentItem = newItem;
this._dataGrid.BeginEdit();
};
}
这会强制第一个单元格立即进入编辑模式.
This forces the first cell to immediately go into edit mode.
这篇关于如何立即验证 Silverlight 3 Datagrid 中新插入的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!