2011-03-17 91 views
1

我有一個wcf服務,它返回一個通用列表:List lstAccount。 我winforms應用程序中的表示層客戶端使用代理對象來連接到此服務。從wcf響應到下拉列表的綁定集合

在代理,賬戶類被這樣產生的:

[System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "3.0.4506.2152")] 
    [System.SerializableAttribute()] 
    [System.Diagnostics.DebuggerStepThroughAttribute()] 
    [System.ComponentModel.DesignerCategoryAttribute("code")] 
    [System.Xml.Serialization.XmlTypeAttribute(Namespace="http://schemas.datacontract.org/2004/07/Test.Common")] 
    public partial class Account {} 

當我結合這個lstAccount在我的表現層的窗體上的下拉列表[通過設置DataSource屬性],在下拉列表只顯示了班級名稱「帳戶」而不是縮寫帳戶名稱。

this.cblExistingAccounts.DataSource = lstAccount; 
this.cblExistingAccounts.DisplayMember = "Name"; 

我在這裏錯過了什麼?

謝謝。

+0

當您測試在wcftestclient將WebMethod你得到的記錄?客戶端應該首先調用代理方法,我在這裏沒有看到... – sajoshi 2011-03-17 07:42:58

+0

是的,我確實從服務獲取記錄。爲簡潔起見,我沒有在此處添加該代碼。 – Jimmy 2011-03-17 07:59:03

回答

1

確保服務端的帳戶類的屬性使用DataMember屬性進行裝飾。

編輯:確保你的代理是最新的,嘗試刷新它,看看它是否修復了這個問題。

+0

我沒有svcUtil工具。我的機器上有DatasvcUtil.exe文件,我可以用它來生成代理嗎? – Jimmy 2011-03-17 08:24:52

+0

如果使用Visual Studio「Service Reference」功能創建代理,可以右鍵單擊它並選擇「更新服務參考」 – 2011-03-17 10:29:03

+0

我在服務器端添加了DataMember屬性,再次生成了proy,但結果相同。我知道添加此屬性後代理對象中會出現什麼差異?謝謝。 – Jimmy 2011-03-17 16:31:33

0

修改樣結合:

this.cblExistingAccounts.DataSource = lstAccount; 
this.cblExistingAccounts.DataTextField = "Name"; 
+0

感謝您的回覆,但我使用System.Windows.Forms.ComboBox與this.cblExistingAccounts.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;它沒有DataTextField屬性。 – Jimmy 2011-03-17 08:13:22