Datagrid中新插入的行

Datagrid中新插入的行

本文介绍了如何立即验证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)
{
键入itemType = typeParameters [0];
object newItem = System.Activator.CreateInstance(itemType);

键入sourceType = typeof(System.Windows.Ria.EntityCollection<>);
类型genericType = sourceType.MakeGenericType(itemType);
System.Reflection.MethodInfo addMethod = genericType.GetMethod(Add);
addMethod.Invoke(this._dataGrid.ItemsSource,new object [] {newItem});

// ==验证数据==
}
}

这是有效的,但是我需要它在新项目添加后也进行验证。有两种方法可以做到这一点:


  1. 强制用户进入编辑模式
    为第一个单元格
    中的新行。 (这将强制
    验证,如果他们点击页面上的任何
    else。)

  2. 强制验证
    立即运行新行
    添加(或当网格丢失
    焦点时)。

我无法获得这些上班。尝试这个,但它只选择该行,不强制验证运行:

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

任何建议?

解决方案

只需从。



我将以下内容添加到== Validate data here ==部分中以上代码:

  DataGridRow newRow = this._dataGrid.ChildrenOfType< DataGridRow>()。FirstOrDefault(); 
if(newRow!= null)
{
newRow.Loaded + =(sender,e)=>
{
this._dataGrid.CurrentItem = newItem;
this._dataGrid.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 modefor the first cell of the new row inthe grid. (This would forcevalidation if they click anywhereelse on the page.)
  2. Force validationsto run immediately when the new rowis added (or when the grid loosesfocus.)

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?

解决方案

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.

这篇关于如何立即验证Silverlight 3 Datagrid中新插入的行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 14:28