2011-10-15 158 views
1

里面我有一个简单的类是这样的:列表框列表框的另一个

public class myClass 
{ 
    public List<string> innerList; 
    public string innerString{get;set;} 
} 

然后,我有这个类的一个对象的List:列出< MyClass的>。对于该列表中的示例中,内容是

myList = new List<myClass>{ 
    new myClass{ 
     innerList = new List<string>{ 
      "1", "2", "3", "4" 
     }, 
     innerString = "First String" 
    }, 
    new myClass{ 
     innerList = new List<string>{ 
      "a", "b", "c", "d" 
     }, 
     innerString = "Second String" 
    } 
}; 

我想显示在下面的表格此数组上下文:

First String 
    1 2 3 

Second String 
    a b c 

注意:实际上我不能连接1 + 2 + 3且a + b + C因为我有什么是不是字符串数组而是一个BitmapImage的数组(这就是为什么我认为我需要另一个列表框此)

所以,我想我应该让另一个列表框内部的列表框。但是可能有更好的方法来做到这一点。

这里我not_working XAML代码:

<ListBox x:Name="myListBox" ItemsSource="{Binding ''}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="{Binding innerString}"/> 
       <ListBox x:Name="innerListBox" ItemsSource="{Binding innerList}"> 
        <ListBox.ItemTemplate> 
         <DataTemplate> 
          <TextBox Text="{Binding ''}"/> 
         </DataTemplate> 
        </ListBox.ItemTemplate> 
       </ListBox> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

它显示只有 “第一字符串” 和 “第二字符串”,而不是 “1 2 3” 和 “A B C”。

我该怎么做才能在另一个数组内显示数组内容?

回答

2

如果您删除内部列表的列表项模板,那么xaml应该可以正常工作。

如果你不提供一个ItemTemplate,那么默认列表项时,它只是调用DataContext,在这种情况下是string

一件事要注意的ToString()方法是,如果你希望您的列表动态更新,您应该使用ObservableCollection<string>而不是List<string>ObservableCollection implements INotifyCollectionChanged,它告诉用户界面添加或删除一个项目。

+0

首先,感谢您通知'ObservableCollection'。非常有用的提示。并且我意识到我的错误在哪里:我忘记了将{get; set;}方法设置到myClass中的List中。你对这个XAML应该有效的评论让我思考并且重新检查我的cs代码)谢谢 – bulbform

+0

很高兴能够提供服务 –

相关问题