![](/img/trans.png)
[英]how can i loop through dynamic ObservableCollection class inside method
[英]How can I add Remove method to ObservableCollection?
我有一個raddataform控件,它使用ObservableCollection作為輸入源並自動生成字段。我已經在person類本身中實現了插入和編輯邏輯,該類通過BeginEdit和EndEdit方法實現了IEditableObject和INotifyPropertyChanged。 但是我不能使用public void Delete()方法。我還了解到ObservableCollection具有CollectionChanged事件,該事件具有NotifyChangedCollectionAction.Remove。那么如何在ObservableCollection上實現delete(remove)邏輯,以便它可以使用linq刪除相應的字段?
這是代碼:
public class EmployeeDataContext
{
private ICollectionView employees = null;
public ICollectionView Employees
{
get
{
if (this.employees == null)
{
ObservableCollection<Person> newEmployees = new ObservableCollection<Person>();
DataClassesDataContext db = new DataClassesDataContext();
var query = from c in db.EPersons
select c;
foreach (var q in query)
{
newEmployees.Add(new Person((DateTime)q.EStartingDate, q.EFirstName,q.ELastName, (Person.OccupationPositions) q.EOccupation,q.EPhoneNumber, (int)q.ESalary));
}
//newEmployees.CollectionChanged += (sender, args) =>
// {
// if (args.Action == NotifyCollectionChangedAction.Remove)
// }
return this.employees;
}
}
}
從BeginEdit和EndEdit背后代碼的事件處理程序的evenargs中提取DataConext對象。
然后調用引用了DataContext的viewmodel的Delete方法。
我不確定100%是否完全理解您的問題,但是如果您要詢問如何向ObservableCollection<T>
類添加自定義Remove
方法,則可以使用Extension Methods 。 也許是這樣的:
public static class ExtensionMethods
{
public static bool Remove<T>(this ObservableCollection<T> collection)
{
var someObject;
// custom logic here
return collection.Remove(someObject);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.