I was working on a proof of concept for binding to an ObservableCollection property which returns the result of a delegate. The delegate has a property set up for it also, so that I can change the expression used, and it triggers OnPropertyChanged for the Collection. This allows me to bind a ComboBox to the Collection, and upon changing the expression/query the available choices in the ComboBox will change too.
Code:
public delegate List<string> Del();
private Del _query;
public Del Query
{
get
{
return _query;
}
set
{
_query= value;
OnPropertyChanged("BindList");
}
}
private ObservableCollection<string> bindList;
public ObservableCollection<string> BindList
{
get
{
var results = Query();
bindList = new ObservableCollection<string>(results);
return bindList;
}
set
{//I believe I need this setter for the extra functionality provided by ObservableCollections over Lists
if(bindList != value) {
bindList = value;
OnPropertyChanged("BindList");
}
}
}
Since this works, I’m wanting to make a class out of it that will be simple to bind to. I’m asking for guidance on how to do so. I’ve thought some about subclassing ObservableCollection, but was having issues with how to set the Items. I’ve also considered just a custom class using interfaces like IEnumerable and ICollectionView (along with the notification Interfaces).
So in summary, how would you build a class to incorporate a collection whose members are based on a delegate query (LINQ to be specific) with regard to subclassing/interfaces?
Thanks in advance.
Here’s what I’ve come up with so far. Hasn’t had a lot of testing yet, but so far its looking pretty good.
It can be bound to in a ComboBox using
ItemsSource="{Binding Path=myCollection, Mode=TwoWay}"(given you haveDyamicCollection myCollectionset up as a property in your ViewModel/data context).It all works by setting the Query, which in my case I give a LINQ to XML query that returns a List. The Collection updates on this, and the bound ComboBox reflects this update.
Please feel free to critique this. I’m fairly certain I’ve got something off, or maybe there is even a better way to do this. I’m open to feedback, and will update this answer as it evolves.