I have run into a problem w/ my model for databinding in WPF. I have an object which has a collection of objects (Variable), and each in turn has yet another collection of objects (VariableCode).
What I need to be able to do is somehow expose a single collection from the highest level object - which is a fusing of the lowest level collections (VariableCode) where each member object meets a condition. So that I can bind that collection to a ListBox, which should display VariableCodes that belong to 1 or more variables.
So the code looks something like this:
public class CrossTab
{
public VariableList Variables{get; set;}
}
public class Variable
{
public VariableCodeList VariableCodes{get; set;}
}
public class VariableCode
{
public bool IsShown{get; set;}
}
What I'd really like to do is expose a property on CrossTab (preferably an ObservableCollection<VariableCode>) which is a view on all the contained VariableCodes where IsShown == true. Currently, they're separate collections - each contained in their own Variable object.
public class CrossTab
{
public ObservableCollection<VariableCode> ShownCodes
{
get
{
//This is where I could picture some brute force looping
// and building of a new collection - but that's not really
// what I'm after. What I want is a live view on the data
// that's in there
}
}
}
The brute force code that I was toying with which returns the correct data - just not as a live view, but rather as a static collection.
ObservableCollection<VariableCode> output = new ...();
Variables.ForEach(v =>
v.VariableCodes.Where(vc => vc.IsShown)
.ForEach(vc => output.Add(vc))
);
return output;
Thoughts? Is such a thing possible?