C# 在源更改时进行LINQ源更新
我有以下LINQ查询,它为分组的C# 在源更改时进行LINQ源更新,c#,linq,windows-runtime,C#,Linq,Windows Runtime,我有以下LINQ查询,它为分组的CollectionViewSource创建源。问题是,当示例更改时(即添加了示例),它不会得到更新。 我不知道如何绑定LINQ查询 cvsExamplesSource.Source = from example in Examples group example by example.Author into grp orderby grp.Key
CollectionViewSource
创建源。问题是,当示例更改时(即添加了示例),它不会得到更新。
我不知道如何绑定LINQ查询
cvsExamplesSource.Source = from example in Examples
group example by example.Author into grp
orderby grp.Key
select grp;
那么,我如何告诉它在示例更改时进行更新,而不必在发生
PropertyChanged
事件时重新加载整个源代码?建议在XAML中绑定cvsExamplesSource.source,并使用新的属性Examples分组,如图所示:
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));
}
}
}
数据上下文类:
<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));
}
}
}
公共类MyClass:INotifyPropertyChanged/*或从ModelViewBase派生*/
{
公共可观测集合示例{get;private set;}
公共IEnumerable示例分组
{
得到
{
从示例中的示例返回
按示例分组。将作者放入grp中
orderby grp.Key
选择玻璃钢;
}
}
公共MyClass()
{
示例=新的ObservableCollection();
Examples.CollectionChanged+=(u,uu)=>RaisePropertyChanged(“ExamplesGrouped”);
}
公共事件属性更改事件处理程序属性更改;
受保护的void RaisePropertyChanged(字符串propertyName)
{
PropertyChangedEventHandler处理程序=PropertyChanged;
if(null!=处理程序)
{
Invoke(这是新的PropertyChangedEventArgs(propertyName));
}
}
}