I have a simple class like this:
public class myClass
{
public List<string> innerList;
public string innerString{get;set;}
}
And then I have a List of objects of this class: List<myClass>. For example content of this list is
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"
}
};
I want to display this array context in the following form:
First String
1 2 3
Second String
a b c
Note: actually I cannot concatenate 1+2+3 and a+b+c because what I have is not string array but an BitmapImage array (thats why I think that I need another listbox for this)
So, I think I should make a listbox inside another listbox. But possibly there is better way to do this exists.
Here my not_working XAML code:
<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>
It displays only “First String” and “Second String”, but not “1 2 3” and “a b c”.
What should I do to display an Array content inside another Array?
if you remove the list item template for the inner list, your xaml should work just fine.
If you do not supply an
ItemTemplate, then the default ListItem is used, which simply calls theToString()method of theDataContext, which in this case is astringone other thing to note, is that if you want your list to dynamically update, you should be using
ObservableCollection<string>instead ofList<string>.ObservableCollectionimplementsINotifyCollectionChanged, wich tells the UI that an item is added or removed.