ListBox在另一个ListBox中

时间:2011-10-15 21:47:39

标签: c# silverlight windows-phone-7 data-binding

我有一个像这样的简单类:

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

然后我有一个这个类的对象列表:List&lt; myClass&gt;。例如,此列表的内容是

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>

它只显示“First String”和“Second String”,但不显示“1 2 3”和“a b c”。

如何在另一个数组中显示数组内容?

1 个答案:

答案 0 :(得分:2)

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

如果您不提供ItemTemplate,则会使用默认的ListItem,它只调用ToString()的{​​{1}}方法,在这种情况下为DataContext }

另外需要注意的一点是,如果您希望列表动态更新,则应使用string而不是ObservableCollection<string>List<string>实现ObservableCollection,告诉用户界面添加或删除项目。

相关问题