LINQ query to Observable collection
I've got an observable collection that contains multiple DocumentEntry objects that each have a language property. I present this in a DataGrid so that the documents can be updated, but that became too many entries, so I've added a combobox with language names and now I need to present only the documents of that language.
The collection of documents is an ObservableCollection, but when I say
myDataGrid.DataContext = (from d in documents where d.language == selectedLanguage select d);
the result of the LINQ query is not an observable collection. Do I filter this the right way at all? How can I best filter an ObservableCollection in my datagrid, in 开发者_JAVA技巧this case by language?
Cheers
Nik
Better to yous CollectionViewSource.Filter
Something like this
myDataGrid.DataContext = documents;
CollectionViewSource cvs = CollectionViewSource.GetDefaultView(documents);
vse.Filter = delegate(object obj)
{
Document doc = obj as Document;
if(doc == null)
return false;
return doc.language == selectedLanguage;
}
Try This,
XAML:
<Listbox x:name="MyLB"/>
CB:
Dim q = from c as myobject in myobservablecollection where c.CriteriaA= CriteriaB
MyLb.Itemsource = q.ToList
Fundamental being : Datacontext can only be set for objects which have been created. As far as LINQ is concerned, You need to pass the LINQ result query as TOLIST to reflect the filtered view .
NOTE: This will not update as ToList is a generic list and has no INotifyPropertyChanged implemented.
精彩评论