LINQ 查询可观察集合

发布于 2024-10-12 03:51:58 字数 396 浏览 7 评论 0原文

我有一个可观察的集合,其中包含多个 DocumentEntry 对象,每个对象都有一个语言属性。我将其呈现在 DataGrid 中,以便可以更新文档,但这变得太多条目,因此我添加了一个带有语言名称的组合框,现在我只需要呈现该语言的文档。

文档集合是一个 ObservableCollection,但是当我说

myDataGrid.DataContext = (from d in documents where d.language == selectedLanguage select d);

LINQ 查询的结果不是一个可观察集合时。我的过滤方式是否正确?如何最好地过滤数据网格中的 ObservableCollection(在本例中是按语言)?

干杯

尼克

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 this case by language?

Cheers

Nik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长发绾君心 2024-10-19 03:51:58

对你更好 CollectionViewSource.Filter

一些东西像这样

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;
}

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;
}
季末如歌 2024-10-19 03:51:58

尝试一下,

XAML:

<Listbox x:name="MyLB"/>

CB:

Dim q = from c as myobject in myobservablecollection where c.CriteriaA= CriteriaB

MyLb.Itemsource =  q.ToList

基本原理:只能为已创建的对象设置数据上下文。就 LINQ 而言,您需要将 LINQ 结果查询作为 TOLIST 传递以反映过滤后的视图。

注意:这不会更新,因为 ToList 是通用列表并且没有实现 INotifyPropertyChanged。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文