当其他 ObservableCollection 改变时 BindableCollection 改变
BindableCollection changes when other ObservableCollection changes
有没有办法用其他 ObservableCollection 中 added/deleted 的项目更新 ObservableCollection?
在 FullyObservableCollection 中添加和删除项目时,如何更新我的 ViewModel 的 BindableCollection?
重要的是要注意我正在尝试将 MVVM 模式与 Caliburn.Micro 一起使用。
VieModel
private BindableCollection<Employees> _employees = new BindableCollection(OracleConnector.GetEmployeeRepositorys());
public BindableCollection<Employees> Employees
{
get
{
return _employees;
}
set
{
OracleConnector.List.CollectionChanged += (sender, args) =>
{
_employees = OracleConnector.List;
};
NotifyOfPropertyChange(() => Employees);
}
}
OracleConnector
public class OracleConnector
{
public static FullyObservableCollection<Employees> List = new FullyObservableCollection<Employees>();
public static FullyObservableCollection<Employees> GetEmployeeRepositorys()
{
using (IDbConnection cnn = GetDBConnection("localhost", 1521, "ORCL", "hr", "hr"))
{
var dyParam = new OracleDynamicParameters();
try
{
var output = cnn.Query<Employees>(OracleDynamicParameters.sqlSelect, param: dyParam).AsList();
foreach (Employees employees in output)
{
List.Add(employees);
}
}
catch (OracleException ex)
{
MessageBox.Show("Connection to database is not available.\n" + ex.Message, "Database not available", MessageBoxButton.OK, MessageBoxImage.Error);
}
return List;
}
}
}
我能够检测到是否在 FullyObservableCollection 中进行了更改,但我不知道如何将它们传递给 ViewModel。
添加新员工时使用 OracleConnector
class 中的 IEventAggregator
。发布包含新员工的 EmployeeAddedMessage
。确保您也在正确的线程上发布。您很可能需要使用 PublishOnUiThread
方法。然后 ShellViewModel
能够将 IHandle<EmployeeAddedMessage>
实现为一种可能称为 Handle(EmployeeAddedMessage msg)
的方法。在 Handle
方法中,您可以将 Employee
添加到适当的 Employee
集合中。
您可能需要将 OracleConnector
添加到您的应用程序引导程序以及 Caliburn Micro 提供的 EventAggregator
class。您的 ShellViewModel
还需要调用事件聚合器上的 Subscribe(this)
方法。 OracleConnector
和 ShellViewModel
都需要使用相同的事件通知程序实例。因此,请确保将事件聚合器注册为单例。
参见 here for more details about using the event notification. Also, my application here 对应用程序事件使用事件通知。
有没有办法用其他 ObservableCollection 中 added/deleted 的项目更新 ObservableCollection?
在 FullyObservableCollection 中添加和删除项目时,如何更新我的 ViewModel 的 BindableCollection?
重要的是要注意我正在尝试将 MVVM 模式与 Caliburn.Micro 一起使用。
VieModel
private BindableCollection<Employees> _employees = new BindableCollection(OracleConnector.GetEmployeeRepositorys());
public BindableCollection<Employees> Employees
{
get
{
return _employees;
}
set
{
OracleConnector.List.CollectionChanged += (sender, args) =>
{
_employees = OracleConnector.List;
};
NotifyOfPropertyChange(() => Employees);
}
}
OracleConnector
public class OracleConnector
{
public static FullyObservableCollection<Employees> List = new FullyObservableCollection<Employees>();
public static FullyObservableCollection<Employees> GetEmployeeRepositorys()
{
using (IDbConnection cnn = GetDBConnection("localhost", 1521, "ORCL", "hr", "hr"))
{
var dyParam = new OracleDynamicParameters();
try
{
var output = cnn.Query<Employees>(OracleDynamicParameters.sqlSelect, param: dyParam).AsList();
foreach (Employees employees in output)
{
List.Add(employees);
}
}
catch (OracleException ex)
{
MessageBox.Show("Connection to database is not available.\n" + ex.Message, "Database not available", MessageBoxButton.OK, MessageBoxImage.Error);
}
return List;
}
}
}
我能够检测到是否在 FullyObservableCollection 中进行了更改,但我不知道如何将它们传递给 ViewModel。
添加新员工时使用 OracleConnector
class 中的 IEventAggregator
。发布包含新员工的 EmployeeAddedMessage
。确保您也在正确的线程上发布。您很可能需要使用 PublishOnUiThread
方法。然后 ShellViewModel
能够将 IHandle<EmployeeAddedMessage>
实现为一种可能称为 Handle(EmployeeAddedMessage msg)
的方法。在 Handle
方法中,您可以将 Employee
添加到适当的 Employee
集合中。
您可能需要将 OracleConnector
添加到您的应用程序引导程序以及 Caliburn Micro 提供的 EventAggregator
class。您的 ShellViewModel
还需要调用事件聚合器上的 Subscribe(this)
方法。 OracleConnector
和 ShellViewModel
都需要使用相同的事件通知程序实例。因此,请确保将事件聚合器注册为单例。
参见 here for more details about using the event notification. Also, my application here 对应用程序事件使用事件通知。