2013-08-02 70 views
0

好吧,所以this question非常好用且易於理解。我想以這種確切的方式將StackPanel實現爲TreeViewItem。但是,當我嘗試設置面板的Orientation時,該comiler抱怨實施IEnumerableIEnumerable並在代碼隱藏中創建StackPanels

這裏是我的TreeViewItem - >StackPanel實現:

public static TreeViewItem newnode = new TreeViewItem() 
{ 
     Header = new StackPanel { 
      Orientation.Horizontal 
     } 
}; 

我還沒有和IEnumerable工作過,但我想通過導入System.Collections,然後設置我的類從IEnumerable繼承實現它。這樣做後,我得到一個編譯器錯誤,說我的班級不執行System.Collections.IEnumerable.GetEnumerator()

看了幾眼online resources我知道了,顯然IEnumerable<T>包含GetEnumerable()

首先,我在正確的軌道上?如果是這樣,我如何正確地獲得此設置?

另外,如果我需要從IEnumerable<T>什麼將我放在<>繼承,如果我不與某種ListTemplate工作?

感謝您的幫助。

的要求

'Project.Folder.Class' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'

+0

你可以顯示你從編譯器獲得的有關實現IEnumerable的確切錯誤嗎? – Gjeltema

+0

是的,我想你可能已經讀了最初的錯誤......它可能並不意味着你實現IEnumerable。 – Sheridan

回答

1

如果你想初始化的對象上的特定屬性,則應該通過命名屬性使用Object Initialiser語法要初始化精確的編譯器錯誤:

TreeViewItem newNode = new TreeViewItem() 
{ 
    Header = new StackPanel { Orientation = Orientation.Horizontal} 
}; 

就你而言,編譯器告訴你無法使用Collection Initializer s初始化StackPanel yntax。

此:

new StackPanel 
{ 
    Orientation.Horizontal 
} 

會產生錯誤,你看到:

Error 1 Cannot initialize type 'System.Windows.Controls.StackPanel' with a collection initializer because it does not implement 'System.Collections.IEnumerable' 

因爲你試圖初始化StackPanel中好像它是System.Windows.Control.Orientation對象的集合,例如List<Orientation>

Object and Collection Initializers

+0

我看到...這是一個語法錯誤,非常感謝! –

+0

適用於我們所有人= D – Chris