【发布时间】:2010-02-03 22:25:36
【问题描述】:
我有一个带有自定义 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();
有什么建议吗?
【问题讨论】:
-
我一直在尝试做类似的事情:stackoverflow.com/questions/2131666/…。我没有自定义库,但我也在代码中创建行。
标签: reflection silverlight-3.0 datagrid c#-3.0 wcf-ria-services