CollectionViewSource
这是我的第一个WPF 应用程序,我需要一些帮助
ObjectQuery<ITPORTAL.HRMS_IT_ASSET_MASTER> ass = gRACHRMSEntities.HRMS_IT_ASSET_MASTER;
并且分配给 CollectionViewSource.Source 的对象查询如下
hRMS_IT_ASSET_MASTERQuery = Load_Asset_Master_Data(gRACHRMSEntities.HRMS_IT_ASSET_MASTER);
hRMS_IT_ASSET_MASTERViewSource.Source = hRMS_IT_ASSET_MASTERQuery.Execute(System.Data.Objects.MergeOption.AppendOnly);
但是这段代码不支持过滤设施,这就是为什么我要添加这样的东西
private ObservableCollection<ITPORTAL.HRMS_IT_ASSET_MASTER> Load_Asset_Master_Data(System.Data.Objects.ObjectQuery<ITPORTAL.HRMS_IT_ASSET_MASTER> objectSet)
{
ObservableCollection<ITPORTAL.HRMS_IT_ASSET_MASTER> oc = null;
try
{
var value = from Asset_Master in objectSet
where Asset_Master.IT_ASSET_IS_ACTIVE == "1" && Asset_Master.IT_ASSET_IS_SCRAPED == "0" && Asset_Master.IT_ASSET_IS_SCRAP_FINAL == "0"
select Asset_Master;
oc = new ObservableCollection<HRMS_IT_ASSET_MASTER>(value.ToList());
}
catch (Exception err)
{
MessageBox.Show(err.Message, "IT Portal Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
}
return oc;
}
并分配给 CollectionViewSource 如下
hRMS_IT_ASSET_MASTERViewSource.Source = Load_Asset_Master_Data(gRACHRMSEntities.HRMS_IT_ASSET_MASTER);
通过这种方式,我可以进行过滤,但我失去了一个功能,因为当我填充数据网格末尾的行并按保存时,保存命令不考虑添加的行,保存命令仅对编辑操作的响应是保存事件处理程序
private void btnSave_Click(object sender, RoutedEventArgs e)
{
try
{
if (gRACHRMSEntities.SaveChanges() > 0)
{
txtblkStatus.Text = "Changes has been saved.";
}
}
catch (Exception err)
{
MessageBox.Show(err.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
} }
我在网上查了一下,发现在使用时ObservableCollection<T>
我必须IEditableObject
为 T提供我自己的实现。