如何立即验证 Silverlight 3 Datagrid 中新插入的行?

发布于 2024-08-20 08:08:33 字数 1277 浏览 1 评论 0原文

我有一个带有自定义 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();

有什么建议吗?

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:

  1. 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.)
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

满意归宿 2024-08-27 08:08:33

感谢 这个问题

我将以下内容添加到上面代码中的“==在此处验证数据==”部分:

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

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

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文