2015-08-15 19 views
0

我試圖定義ListBox來顯示我在某個集合中定義的所有元素。看不到我列表框中的項目

代碼:

public class Element 
{ 
    private string _name; 
    public string Name { get { return _name; } } 

    public Element(string name) 
    { 
     _name = name; 
    } 
} 

public class ElementCollection 
{ 
    public List<Element> Elements 
    { 
     get; 
     set; 
    } 
} 





    <ListBox ItemsSource="{Binding ElementCollection}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <StackPanel Orientation="Vertical"> 
        <TextBlock Text="{Binding Element.Name}"/> 
       </StackPanel> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

我做 '的DataContext',所有的數據都在正確的地方。

我做錯了什麼?

回答

1

您的binding是持有該集合的類,而不是實際的集合 - 請嘗試將您的ItemSource更改爲{Binding ElementCollection.Elements}。此外,您Textbox勢必Element.Name,你不需要這些,因爲這將查找在您的項目Name屬性:

<ListBox ItemsSource="{Binding ElementCollection.Elements}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Vertical"> 
       <TextBlock Text="{Binding Name}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

嘗試..不工作 – Yanshof

+0

現在看看,剛剛發現另一件事是WASN」 t right – Horia

+0

still still work :( – Yanshof

相關問題