【问题标题】:How do I Immediately Validate a Newly Inserted Row in a Silverlight 3 Datagrid?如何立即验证 Silverlight 3 数据网格中新插入的行?
【发布时间】: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 ==
    }
}

这可行,但我还需要在添加新项目后对其进行验证。我可以看到有两种方法可以做到这一点:

  1. 强制用户进入编辑模式 对于新行的第一个单元格 网格。 (这将迫使 验证他们是否点击任何地方 页面上的其他内容。)
  2. 强制验证 新行时立即运行 添加(或当网格松动时 重点。)

我无法让其中任何一个工作。试过了,但它只选择行,不强制运行验证:

this._dataGrid.SelectedItem = newItem;
System.ComponentModel.IEditableObject editableItem = newItem as System.ComponentModel.IEditableObject;
if (editableItem != null)
    editableItem.BeginEdit();

有什么建议吗?

【问题讨论】:

标签: reflection silverlight-3.0 datagrid c#-3.0 wcf-ria-services


【解决方案1】:

感谢this question 的一些帮助,这才可以正常工作。

我在上面代码中的“== Validate data here ==”部分添加了以下内容:

DataGridRow newRow = this._dataGrid.ChildrenOfType<DataGridRow>().FirstOrDefault();
if (newRow != null)
{
    newRow.Loaded += (sender, e) =>
    {
        this._dataGrid.CurrentItem = newItem;
        this._dataGrid.BeginEdit();
    };
}

这会强制第一个单元格立即进入编辑模式。

【讨论】:

    猜你喜欢
    • 2010-12-10
    • 2017-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多