如何立即验证 Silverlight 3 Datagrid 中新插入的行?
我有一个带有自定义 DataGrid 用户控件的 Silverlight 3 工具库。该网格无法直接访问 WCF RIA 服务实体类型,因此当用户在网格为空时单击网格时,我使用反射来添加新项目:
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._dataGrid.SelectedItem = newItem;
System.ComponentModel.IEditableObject editableItem = newItem as System.ComponentModel.IEditableObject;
if (editableItem != null)
editableItem.BeginEdit();
有什么建议吗?
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:
- Force the user to enter edit mode
for the first cell of the new row in
the grid. (This would force
validation if they click anywhere
else on the page.) - Force validations
to run immediately when the new row
is added (or when the grid looses
focus.)
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();
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
感谢 这个问题。
我将以下内容添加到上面代码中的“==在此处验证数据==”部分:
这会强制第一个单元格立即进入编辑模式。
Just got this working thanks to some help from this question.
I added the following to the "== Validate data here ==" section in the code from above:
This forces the first cell to immediately go into edit mode.