2016-06-12 138 views
0

我目前正在幫助客戶創建InfoPath和Im的表單,並且遇到了一些問題,讓我的列表像我想要的那樣工作。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

有什麼建議?

回答

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(""); 
} 
0

嘗試使用

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

應該做同樣的工作作爲內置的InfoPath 「添加元素」。只需將「XmlToEdit」替換爲要插入的組的名稱即可。

+0

感謝您的回覆。使用InfoPath 2010,我沒有在CurrentView下獲得ExecuteAction –

相關問題