如何将两个ObservableCollection绑定到一个ListBox?

时间:2012-09-22 10:42:34

标签: wpf

有人知道如何在一个ListBox中绑定两个ObservableCollections吗? 这两个ObservableCollections都有一个属性字符串“name”显示在ListBox中, enter image description here

在ListBox顶部区域,将显示ObservableCollection1项目,并在ListBox底部区域中我想显示ObservableCollection2项目,该怎么做?

<ListBox x:Name="m_CtrlMediaList" Grid.Column="2" AllowDrop="True" SelectionMode="Extended">
    <ListBox.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding directorys}"/>
            <CollectionContainer Collection="{Binding files}"/>
        </CompositeCollection>
    </ListBox.ItemsSource>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Vertical">
                <TextBlock Text="{Binding name, Mode=OneWay}" FontWeight="Bold" FontSize="14"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

数据如下:     class ElementFile     {     ...           字符串名称(get; set;}     ...

}
 class ElementDirectory
{
...
      string name (get;set;}
 ...
    public ObservableCollection<ElementDirectory> directorys { get; set; }
    public ObservableCollection<ElementFile> files { get; set; }
...
}

为什么无法显示“名称”?

1 个答案:

答案 0 :(得分:0)

composite collection正是您要找的。 Example

相关问题