1

In this scenario I have a collection of objectA, each of which has a property which is a collection of objectB.

I am attempting to properly bind to the collection of objectB collections to present into a list, or in other words, I want the listbox to contain all of the objectBs that all of the objectAs contain.

My attempts at binding as such "{Binding objectACollection/objectBCollection} but this does not provide me the result.

Mike Schwartz
  • 2,182
  • 1
  • 17
  • 26
Michael Flanagan
  • 315
  • 1
  • 3
  • 7

3 Answers3

1

I want the listbox to contain all of the objectBs that all of the objectAs contain.

I think you want common items from two lists and if it is the case then below code can help you

var commonElements = objectAs.Intersect(objectBs).ToList();
Mukesh Rawat
  • 2,047
  • 16
  • 30
0

You are trying to mix Presentation and Logic, which I feel is resulting in confusion.

If you are having a collection withing a collection and want to display it in a tree structure use Hierarchichal Datatemplate. You can use Tree/List control whichever suits you. e.g.:TreeView, HierarchicalDataTemplate and recursive Data

As far as I understand your question, you are trying to create a union of objectACollection and objectBCollection and display it as a single list. I suggest you to expose a property in your view model which creates this union rather than attempting it at the View. Because this is clearly a logic which belongs to the ViewModel rather than making the XAML/View to figure it out.

Please correct if my understanding is wrong.

Community
  • 1
  • 1
Carbine
  • 7,849
  • 4
  • 30
  • 54
  • You're correct on the second thought, I ended up creating just such a property, I was just hoping there'd be a relatively simple front end way to do this, but the incredibly simple back end way works just as well, thanks a lot. – Michael Flanagan Feb 11 '14 at 21:39
0
public class TheDataContext
{
    public TheDataContext()
    {
        _listOfAs = new List<A>
        {
            new A(),
            new A(),
            new A() 
        };
        _listOfBs = new ObservableCollection<B>(_listOfAs.SelectMany(a => a.B));
    }

    private List<A> _listOfAs;

    private ObservableCollection<B> _listOfBs;
    public ObservableCollection<B> ListOfBs
    {
        get
        {
            return _listOfBs;
        }
    }

}

public class A
{
    public A()
    {
        B = new List<B>
        {
            new B(),
            new B(),
            new B()
        };
    }
    public IEnumerable<B> B { get; set; }
}

public class B
{

}

Since you don't provide any real code, and your question is kind of vague, I guess this is what you mean. This will give you a list of 9 Bs to display in you ListBox.

Johan
  • 101
  • 4