c# - Binding not Updating after Invoking PropertyChanged -
i'm developing kind of time tracking application.
the model looks this:
my datagrid bound
observablecollection<entry>
:
public class entry : inotifypropertychanged { public string ticketid {get; set;} //simplified since working.. private observablecollection<timebooking> _timebookings; public observablecollection<timebooking> timebookings { { return _timebookings; } set { _timebookings = value; onpropertychanged(); } } public timebooking monday //same other days... { { return bookings.where(b => b.date == selectedweek.firstday); } } }
timebooking looks this:
public class timebooking : inotifypropertychanged { public timebooking(datetime date) { date = date.date; } public datetime date { get; set; } private timespan _value; public timespan value { { return _value; } set { _value = value; onpropertychanged(); } } private bool _changed; public bool changed //changed setter invoked cell context menu { { return _changed; } set { _changed = value; onpropertychanged(); } } #region implementation of inotifypropertychanged public event propertychangedeventhandler propertychanged; private void onpropertychanged([callermembername] string propertyname = null) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } #endregion implementation of inotifypropertychanged }
the issue in here:
<datagrid.cellstyle> <style targettype="datagridcell"> <style.triggers> <datatrigger binding="{binding relativesource={relativesource self}, converter={staticresource editedconverter}, updatesourcetrigger=explicit}" value="true"> <setter property="foreground" value="pink" /> </datatrigger> <datatrigger binding="{binding relativesource={relativesource self}, converter={staticresource editedconverter}, updatesourcetrigger=explicit}" value="false"> <setter property="foreground" value="lime" /> </datatrigger> </style.triggers> <setter property="contextmenu" value="{staticresource cellcontextmenu}" /> </style> </datagrid.cellstyle>
this converter:
public object convert(object value, type targettype, object parameter, cultureinfo culture) { datagridcell dgc = value datagridcell; if(dgc != null) { entry entry = dgc.datacontext _entry; if(entry != null) { datagridtextcolumn column = dgc.column datagridtextcolumn; if(column != null) { var binding = column.binding binding; if(binding != null && binding.path != null && binding.path.path != null) { string val = binding.path.path.tolower(); if (val.startswith("monday")) { return entry.monday.changed; } if (val.startswith("tuesday")) { return entry.tuesday.changed; } if (val.startswith("wednesday")) { return entry.wednesday.changed; } if (val.startswith("thursday")) { return entry.thursday.changed; } if (val.startswith("friday")) { return entry.friday.changed; } if (val.startswith("saturday")) { return entry.saturday.changed; } if (val.startswith("sunday")) { return entry.sunday.changed; } } } } } return false; }
as see binding
changed
working (foreground lime) somehow changes property not noticed..
if want converter invoked whenever changed
property set new value, use a multibinding
, multi converter (implement imultivalueconverter
):
<style targettype="datagridcell"> <setter property="foreground" value="pink" /> <style.triggers> <datatrigger value="false"> <datatrigger.binding> <multibinding converter="{staticresource multiconverter}"> <binding path="changed" /> <binding path="." relativesource="{relativesource self}" /> </multibinding> </datatrigger.binding> <setter property="foreground" value="lime" /> </datatrigger> </style.triggers> <setter property="contextmenu" value="{staticresource cellcontextmenu}" /> </style>
public object convert(object[] values, type targettype, object parameter, cultureinfo culture) { datagridcell dgc = values[1] datagridcell; if (dgc != null) { entry entry = dgc.datacontext _entry; if (entry != null) { datagridtextcolumn column = dgc.column datagridtextcolumn; if (column != null) { var binding = column.binding binding; if (binding != null && binding.path != null && binding.path.path != null) { string val = binding.path.path.tolower(); if (val.startswith("monday")) { return entry.monday.changed; } if (val.startswith("tuesday")) { return entry.tuesday.changed; } if (val.startswith("wednesday")) { return entry.wednesday.changed; } if (val.startswith("thursday")) { return entry.thursday.changed; } if (val.startswith("friday")) { return entry.friday.changed; } if (val.startswith("saturday")) { return entry.saturday.changed; } if (val.startswith("sunday")) { return entry.sunday.changed; } } } } } return false; }
}
wiki
Comments
Post a Comment