C# 使CollectionViewSource无效

C# 使CollectionViewSource无效,c#,.net,wpf,xaml,C#,.net,Wpf,Xaml,我定义了以下视图: <CollectionViewSource x:Key="PatientsView" Source="{Binding Source={x:Static Application.Current}, Path=Patients}"/> 如果患者是以下财产: public IEnumerable<Patient> Patients { get { return from patient in Database.Patien

我定义了以下视图:

<CollectionViewSource x:Key="PatientsView" Source="{Binding Source={x:Static Application.Current}, Path=Patients}"/>
如果患者是以下财产:

public IEnumerable<Patient> Patients
{
    get
    {
        return from patient in Database.Patients
               orderby patient.Lastname
               select patient;
    }
}
在代码的某个地方,我更改了Patients数据库,我想让使用PatientsView显示这些数据的控件自动得到通知。这样做的正确方法是什么?
CollectionViewSource是否会失效或发生其他情况?

表不支持IListChanged事件,您必须自己执行此操作,我今天早些时候也必须执行此操作。

如何在代码隐藏中使CollectionViewSource失效:

CollectionViewSource patientsView = FindResource("PatientsView") as CollectionViewSource;
patientsView.View.Refresh();

我认为这比看起来要复杂一些。向客户机应用程序通知数据库中的更改是一项非常重要的任务。但是,如果只从应用程序更改数据库,您的生活会更轻松—这使您能够在更改数据库时放入刷新逻辑

您的Patients属性似乎出现在一个类中,可能不止一个?:。您可能会将一些列表框绑定到CollectionViewSource。因此,您可以让WPF重新调用getter,而不是在CollectionViewSource上调用Refresh。为此,具有Patients属性的类必须实现INotifyPropertyChanged接口

代码如下所示:

public class TheClass : INotifyPropertyChanged
{
public IEnumerable<Patient> Patients
  {
    get
    {
            return from patient in Database.Patients
                   orderby patient.Lastname
                   select patient;
    }
  }

#region INotifyPropertyChanged members
// Generated code here
#endregion

public void PatientsUpdated()
{
  if (PropertyChanged != null)
    PropertyChanged(this, "Patients");
}
}
现在,在类的实例上调用PatientsUpdate以触发绑定的更新


顺便说一句,我觉得这是一个糟糕的设计。

我试过这段代码。调用刷新时不会发生任何事情!我在我的Patients属性上设置了一个断点:调用Refresh时不会调用它。我的所有更改都在应用程序中本地完成,因此我可以添加刷新逻辑。但是,调用刷新没有任何效果!我用更多的想法修改了我的帖子。就是这样。仍然不知道为什么刷新不起作用,但这很好!感谢不要在CollectionViewSource上调用Refresh,您可以让WPF重新调用getter。为什么您认为更新绑定比简单的“CollectionViewSource.View.Refresh”调用更好?