0

I've two ObservableCollection that I've to display in the same WPF ListBox(or similar, I'm quite open.

So in my ViewModel, I've

public ObservableCollection<MySubViewModel> ListA {get;set;}
public ObservableCollection<MySubViewModel> ListB {get;set;}

I know that I could create a new Property which combines the 2 collections, but the thing is:

  • Those 2 collections will have new/removed elements
  • I always need to have the object of the collection 1 to be on the top, and the collection 2 on the bottom
  • I was thinking/hopping/praying that there was a solution

I already tried to use `CompositeCollection, but I don't have the feeling this works with the Observable part?

<ListBox>
    <ListBox.ItemTemplate>
            <TextBlock Margin="5" Text="{Binding Name}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
    <ListBox.ItemsSource>
        <CompositeCollection>
            <CollectionContainer Collection="{Binding ListA }" />
            <CollectionContainer Collection="{Binding ListB }" />
        </CompositeCollection>
    </ListBox.ItemsSource>
</ListBox>

I also tried to have 2 ListBox but I've to have only one of those 2 collection selected, and I need that they are visually looking to be in the same group.

So what you think I could do?

J4N
  • 19,480
  • 39
  • 187
  • 340
  • "I don't have the feeling this works with the Observable part" - what does that mean? Does it work or not? When you say you already tried CompositeCollection, didn't you at least also try to add or remove items to/from both source collections? – Clemens Jul 24 '18 at 12:08
  • @Clemens I did, but I just noticed now, that I've binding errors. For some reason, it appears that it cannot find the FrameworkElement and its dataContext: `Cannot find governing FrameworkElement or FrameworkContentElement for target element. BindingExpression:Path=ListA ; DataItem=null; target element is 'CollectionContainer' (HashCode=22037350); target property is 'Collection' (type 'IEnumerable')` – J4N Jul 24 '18 at 12:23
  • 1
    This should work: https://stackoverflow.com/a/19271605/1136211 – Clemens Jul 24 '18 at 12:33
  • @Clemens this was my issue, could you maybe add an answer with this? – J4N Jul 25 '18 at 06:21
  • The question should better be closed as a duplicate. – Clemens Jul 25 '18 at 07:41
  • @Clemens I did vote for its closure – J4N Jul 25 '18 at 09:01

0 Answers0