所以我的问题是:是否有查询完成事件???
我正在使用实体框架从数据库中查询一些数据并将其显示在 WPF 应用程序中。我的问题是渲染在实体框架从数据库服务器收到结果之前完成,因此我在 UI 上的数据列表仍然是空的。相反,如果我进入调试模式并在继续列出数据之前稍等片刻,则查询完成并列出数据。
我应该如何解决这个问题?
编辑: 这是我的代码:
public class DatabaseModel : DbContext, INotifyPropertyChanged
{
public ObservableCollection<Employee> observableCollection;
public event PropertyChangedEventHandler PropertyChanged;
public DbSet<Employee> Employees { get; set; }
public ObservableCollection<Employee> ObservableEmployees
{
get
{
return observableCollection;
}
set
{
observableCollection = value;
OnPropertyChanged("ObservableEmployees");
}
}
public DatabaseModel()
{
ObservableEmployees = Employees.Local;
}
// Create the OnPropertyChanged method to raise the event
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
以及我如何通过 WPF 使用我的 DataBinding:在 App.Xaml 中,我定义了这个类的一个实例......
<Application.Resources>
<local_database:DatabaseModel x:Key="DatabaseInstance"/>
</Application.Resources>
并使用 Mainwindow.XAML 中的数据
<ListBox ItemsSource="{Binding Source={StaticResource DatabaseInstance}, Path=ObservableEmployees, NotifyOnSourceUpdated=True}"
DisplayMemberPath="Name"
Binding.SourceUpdated="ListBox_SourceUpdated">
</ListBox>