Search code examples
wpfbindingtabcontrolmultibinding

WPF binding from different sources


I'm projecting pretty simple, but effective UI for my app. The problem is that after some actions, lets say, refactoring, I came to re-project my design.

Previously I have had ObsrvableCollection<UsbDevice>. And my tab-control (with complicated custom style..) was binded to that collection. But now I need also something like 'Library' tab item to be there among with connected USB devices. Library IS NOT a collection, but just an business object, which holds some important XML data inside. Id like to use my tab control 'cause otherwise it will be necessary to invent some switching-logic: I can not let both USB device and Library to be selected at the same time.

So, my question is: 1) Maybe some of you, WPF GURUS, know the tricky way of binding from different source? the most obvious decision is to make something like ObservableColelction<object> somewhere else, but this is extremely ugly as for me. 2) Or, if that is impossible, maybe some of you has more easier solution for the above problem?


Solution

  • If I understood you correctly you can use CompositeCollection. In your TabControl:

    <TabControl ...>
        <TabControl.ItemsSource>
            <CompositeCollection>
                <CollectionContainer Collection="{Binding UsbDevicesCollection}"/>
                <TabItem DataContext="{Binding LibraryViewModel}"/>
            </CompositeCollection>
        </TabControl.ItemsSource>
    </TabControl>
    

    Of course you have to set proper names for your collection and library object.