wpf - Binding to an initially NULL property in the ViewModel doesn't rebind -
wpf - Binding to an initially NULL property in the ViewModel doesn't rebind -
i have usercontrol contains telerik raddataform. form's itemssource bound property on usercontrol's viewmodel:
<telerik:raddataform itemssource="{binding path=viewmodel.items, relativesource={relativesource ancestertype=local:myusercontrol}}" />
where viewmodel is:
public partial class myusercontrol: usercontrol { public myusercontrolvm viewmodel { { homecoming this.datacontext myusercontrolvm; } } }
within viewmodel, items ordinary collection:
public class myusercontrolvm : myviewmodelbase { private observablecollection<anitem> items_; public observablecollection<anitem> items { { homecoming this.items_; } set { this.items_ = value; notifypropertychanged("items"); } } ... }
and where, of course, myviewmodelbase implements inotifypropertychanged.
the user command has items dependency property, , when set, sets matching property on view model:
public partial class myusercontrol : usercontrol { public observablecollection<anitem> items { { homecoming getvalue itemsproperty observablecollection<anitem>; } set { setvalue(itemsproperty, value); } } public static readonly dependencyproperty itemsproperty = dependencyproperty.register("items", typeof(observablecollection<anitem>), typeof(myusercontrol), new propertymetadata( new propertychangedcallback(itemspropertychanged))); private static void itemspropertychanged(dependencyobject d, dependencypropertychangedeventargs e) { myusercontrol myusercontrol = d myusercontrol; observablecollection<anitem> items = e.newvalue observablecollection<anitem>; if (myusercontrol != null && myusercontrol.viewmodel != null) myusercontrol.viewmodel.items = items; } }
all of seems pretty straightforward, if bit tedious.
the problem items dependency property on myusercontrol bound property of current item of collection, , current item null, , when myusercontrol loaded, items property null. , hence, items property on myusercontrolvm raddataform binding to.
later, when item in outer collection made current, items dependency property on myusercontrol set, , sets items property on myusercontrolvm. , myusercontrolvm calls notifypropertychanged listeners informed of change. lastly not working.
afterwards, if examine raddataform, itemssource property still null.
it's raddataform isn't listening propertychanged event, because bound null. in similar circumstances bound property not null @ start, pattern works fine current item changes 1 item another, doesn't seem work having no current item having one.
so, ideas how create work? can't, given circumstances, create items has value when form loads - going null, @ start. how raddataform notice when property becomes non-null?
when want reference @ root of usercontrol
(a custom property, instance, or in case, datacontext
), give usercontrol name
. utilize name elementname property on binding
set up.
<usercontrol ... name="thecontrol"> <grid> <textblock text={binding path=datacontext.items, elementname=thecontrol}" /> </grid> </usercontrol>
due viewmodel
property, can utilize , datacontext
interchangeably.
however, in case might simpler. first, there's typo in code. should ancestortype
(with 'o'). second, might want seek setting binding using {binding path=items}
since believe command inherits right datacontext
. (not sure lastly one, though.)
if problem persists, , suspect in fact has items
property returning null
initially, initialize items_
empty collection avoid null
.
private observablecollection<anitem> items_ = new observablecollection<anitem>();
wpf xaml binding
Comments
Post a Comment