当源代码更改时,进行LINQ源代码更新
本文关键字:源代码 进行 LINQ 更新 | 更新日期: 2023-09-27 18:16:11
我有以下LINQ查询,它为分组CollectionViewSource
创建源。问题是,它没有得到更新时,例子的变化(即例子被添加)。我不知道如何绑定一个LINQ查询。
cvsExamplesSource.Source = from example in Examples
group example by example.Author into grp
orderby grp.Key
select grp;
那么,我如何告诉它更新每当示例改变而不必重新加载整个源每当PropertyChanged
事件发生?
建议绑定cvsExamplesSource。XAML中的源,添加新属性ExamplesGrouped,如下所示:
XAML:
<SomeList x:Name="cvsExamplesSource" Source="{Binding ExamplesGrouped}"/>
数据上下文类:
public class MyClass : INotifyPropertyChanged /*or derive from ModelViewBase*/
{
public ObservableCollection<Example> Examples { get; private set; }
public IEnumerable<IGrouping<String, Example>> ExamplesGrouped
{
get
{
return from example in Examples
group example by example.Author into grp
orderby grp.Key
select grp;
}
}
public MyClass()
{
Examples = new ObservableCollection<Example>();
Examples.CollectionChanged += (_, __) => RaisePropertyChanged("ExamplesGrouped");
}
public event PropertyChangedEventHandler PropertyChanged;
protected void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (null != handler)
{
handler.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}