c# - Access ObservableCollection items from property set -


given following class used wpf datacontext:

class viewmodel {     public observablecollection<task> tasks { get; set; } } 

and task class:

public class task {     private string starttime;      public task id { get; set; }     public string starttime     {         { return starttime; }         set         {             // access observablecollection items             starttime = value;             onpropertychanged("starttime");         }     } } 

how can i, @ place in code see "// access observablecollection items", access other items in tasks observablecollection can compare starttime of instance being set starttime of other task items in tasks observablecollection?

you have 2 options:

  1. you can inject viewmodel task

    public class task {     private viewmodel viewmodel;     public class task(viewmodel viewmodel)     {         this.viewmodel = viewmodel;     }     ... } 

    however, makes task class usable viewmodel class, because loosely coupled.

  2. you can perfom starttime validation in viewmodel. need catch propertychanged events of tasks.

    i created extendedobservablecollection fires event when property of item changes:

    public class extendedobservablecollection<t> : observablecollection<t> t : inotifypropertychanged {     public event propertychangedeventhandler itempropertychanged;     protected override void clearitems()     {         foreach (var item in items) item.propertychanged -= onitempropertychanged;         base.clearitems();     }      protected override void insertitem(int index, t item)     {         item.propertychanged += onitempropertychanged;         base.insertitem(index, item);     }      protected override void removeitem(int index)     {         this[index].propertychanged -= onitempropertychanged;         base.removeitem(index);     }      protected override void setitem(int index, t item)     {         this[index].propertychanged -= onitempropertychanged;         item.propertychanged += onitempropertychanged;         base.setitem(index, item);     }      protected virtual void onitempropertychanged(object sender, propertychangedeventargs e)    {        var handler = itempropertychanged;        if (handler != null) handler(sender, e);    } } 

    and finaly validation or whatever need

    public class viewmodel {    public extendedobservablecollection<task> tasks { get; set; }    public viewmodel()    {        tasks=new extendedobservablecollection<task>();        tasks.itempropertychanged += taskpropertychanged;    }     private void taskpropertychanged(object sender, propertychangedeventargs e)    {        var changedtask = (task)sender;        if (e.propertyname == "starttime")        {           if (!isstarttimegreatedthenprevious(changedtask ))                changedtask.seterror("starttime", "start time has greated in previous task)        }    }         } 

you can add or remove items task collection , extendedobservablecollection takes care of attaching/detaching propertychanged event.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -