c# - How to fire Property Changed Event when it's not actually changing -
so have model contains 2 variables, list , datetime. in usercontrol have dependencyproperty , defined propertychangedcallback.
public static readonly dependencyproperty myproperty = dependencyproperty.register("my", typeof(list<mycontainer>), typeof(uc), new frameworkpropertymetadata(null, new propertychangedcallback(onmyproperty))); public list<mycontainer> { { return getvalue(myproperty) list<mycontainer>; } set { setvalue(myproperty, value); } } private static void onmyproperty(dependencyobject d, dependencypropertychangedeventargs e) { uc control = d uc; //do stuff }
on form there button, changes on other model variable (on datetime).
private void date_click(object sender, routedeventargs e) { mymodel model = datacontext mymodel; if (model != null) { model.date = model.date.adddays(1); } }
and here model.
public class mymodel : inotifypropertychanged { private list<mycontainer> _my; private datetime _date; public mymodel() { _date = datetime.now.date; _my = new list<mycontainer>(); } public list<mycontainer> { { return _my; } set { _my = value; onpropertychanged("my"); } } public datetime date { { return _date; } set { _date = value; onpropertychanged("date"); onpropertychanged("my"); } } #region inotifypropertychanged public event propertychangedeventhandler propertychanged; private void onpropertychanged(string propertyname) { propertychanged?.invoke(this, new propertychangedeventargs(propertyname)); } #endregion }
xaml declaration following.
<local:uc my="{binding my}" />
so problem after hit run, fires onmyproperty once, after if hit button, changes datetime property well, onmyproperty callback doesn't firing again. noticed if modify model this
public datetime date { { return _date; } set { _date = value; _my = new list<mycontainer>(_my); //added onpropertychanged("date"); onpropertychanged("my"); } }
now fires every time when hit button. how can trigger second behaviour without modification?
the answer not need this. when ask question how make framework not want do, why think need that. it's there's easier answer else using.
the thing have bound control my
. therefore, if my
hasn't changed, state of control should not change. if want state of control change when date
changes, bind date
property of control. only way control should ever information viewmodel through binding 1 of dependency properties property of viewmodel.
the control should not ever know or care or providing values properties. should able job knowing property values has been given.
if contents of my
have changed -- added item or removed 1 -- of course control has no way of knowing that, because refused tell it. you're telling there's new list. checks, sees it's still got same old list, , ignores you. my
property of viewmodel should observablecollection
, because notify control when add or remove items in collection.
the items themselves, mycontainer
class, must implement inofitypropertychanged
well, if want able change properties while displayed in ui.
the dependency property my
on control must not of type list<t>
. should type object
, itemscontrol.itemssource
. control template can display in itemscontrol
knows it. if observablecollection
bound suggested above, itemscontrol
update automatically. in onmyproperty
, control class can check see if it's observable collection well:
private static void onmyproperty(dependencyobject d, dependencypropertychangedeventargs e) { uc control = d uc; if (e.newvalue inotifycollectionchanged) { (e.newvalue inotifycollectionchanged).collectionchanged += (s, ecc) => { // stuff uc , ecc.newitems, ecc.olditems, etc. }; } }
Comments
Post a Comment