InfoPath以编程方式将新行添加到列表的末尾

时间:2016-06-12 12:07:31

标签: c# xpath infopath

我目前正在帮助客户创建一个包含InfoPath的表单,并且我在使列表按照我的意愿行事时遇到了一些问题。

每当我向列表添加一个新元素(重复部分)时,它最终会出现在视图中的列表顶部,我希望它被添加到底部。我的客户想要一个自定义按钮来触发添加元素而不使用"添加元素" InfoPath提供的文本。

这是一个更好地解释我的问题的例子:

enter image description here

当用户在输入字段中写入某个内容时,我希望将其添加到重复部分的列表中。以下是示例代码:

private XPathNavigator GetField(string xPath)
{
    return MainDataSource.CreateNavigator()
                         .SelectSingleNode(xPath, NamespaceManager);
}

public void CTRL10_5_Clicked(object sender, ClickedEventArgs e)
{
    string xPathToList = "/my:myFields/my:group5/my:group6/my:group7";
    string xPathToInput = "/my:myFields/my:group5/my:field2";
    string xPathToListElement = xPathToList + "/my:field3";

    //Creates a new row
    XPathNavigator list = GetField(xPathToList);
    XPathNavigator newRow = list.Clone();
    newRow.InsertAfter(list);

    //Sets values on the new row
    XPathNavigator input = GetField(xPathToInput);
    XPathNavigator nameField = GetField(xPathToListElement);
    nameField.SetValue(input.Value);
    input.SetValue("");
}

当我向列表添加新元素时,它会添加到列表顶部,而不是底部.. enter image description here

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

尝试使用

CurrentView.ExecuteAction(ActionType.XCollectionInsert, "XmlToEdit");

应该与内置的infopath“添加元素”完成相同的工作。只需将“XmlToEdit”替换为您要插入的组的名称。

答案 1 :(得分:0)

我使用它的解决方案是获取XPath表达式列表中的最后一个([last()])元素,并将元素添加到指定元素之后。

private XPathNavigator GetField(string xPath)
{
     return MainDataSource.CreateNavigator()
                          .SelectSingleNode(xPath, NamespaceManager);
}

public void CTRL10_5_Clicked(object sender, ClickedEventArgs e)
{
    string xPathToList = "/my:myFields/my:group5/my:group6/my:group7[last()]";
    string xPathToInput = "/my:myFields/my:group5/my:field2";
    string xPathToListElement = xPathToList + "/my:field3";

    //Creates a new row
    XPathNavigator list = GetField(xPathToList);
    XPathNavigator newRow = list.Clone();
    newRow.InsertAfter(list);

    //Sets values on the new row
    XPathNavigator input = GetField(xPathToInput);
    XPathNavigator nameField = GetField(xPathToListElement);
    nameField.SetValue(input.Value);
    input.SetValue("");
}
相关问题