c# - WPF binding to a Usercontrol and getting an error -
im starting wpf, sorry if cant explain well, , have hours trying solve how bind collection custom grid named pagingdatagrid.
the pagingdatagrid in customersearchcontrol binding griditems itemssource, when excecute searchcommand griditems gets updated nothing else changes.
i following error:
system.windows.data error: 40 : bindingexpression path error: 'griditems' property not found on 'object' ''pagingdatagridviewmodel' (hashcode=54151655)'. bindingexpression:path=griditems; dataitem='pagingdatagridviewmodel' (hashcode=54151655); target element 'pagingdatagrid' (name='me'); target property 'itemssource' (type 'ienumerable')
customersearchcontrol:
<usercontrol x:class="namespace.customersearchcontrol" ... > <control.datacontext> <binding path="customersearchviewmodel" ... /> </control.datacontext> <dockpanel lastchildfill="true"> <groupbox header="registros"> <controls:pagingdatagrid itemssource="{binding griditems}" height="300" /> </groupbox> </dockpanel> </usercontrol> public class customersearchviewmodel : viewmodelbase { public observablecollection<griditem> griditems{ get; set; } public icommand searchcommand { get; set; } public customersearchviewmodel() { griditems = new observablecollection<griditem>(); searchcommand = new relaycommand(searchentities, () => true); } }
pagingdatagrid:
<usercontrol x:class="namespace.pagingdatagrid" x:name="me" ... > <usercontrol.datacontext> <binding path="pagingdatagridviewmodel" ... /> </usercontrol.datacontext> <grid> ... <xcdg:datagridcontrol itemssource="{binding elementname=me, path=itemssource}" grid.row="0"/> </grid> </usercontrol> public partial class pagingdatagrid : usercontrol { public static readonly dependencyproperty itemssourceproperty = dependencyproperty.register("itemssource", typeof(ienumerable), typeof(pagingdatagrid), new propertymetadata(default(ienumerable))); public ienumerable itemssource { { return (ienumerable)getvalue(itemssourceproperty); } set { setvalue(itemssourceproperty, value); } } }
you need declare instance of customersearchviewmodel
in xaml , bind datacontext.
this how it:
<usercontrol.datacontext> <local:customersearchviewmodel/> </usercontrol.datacontext>
make sure declare namespace local
@ root i.e. @ usercontrol:
xmlns:local="clr-namespace:wpfapplication" <-- replace wpfapplication actual namespace of viewmodel.
not needed since getting instance servicelocator.
and binding griditems
need bind explicitly customersearchcontrol
datacontext using relativesource
. needed because have explicitly set datacontext on pagingdatagrid
pagingdatagridviewmodel
. so, search griditems
property in pagingdatagridviewmodel instead of customersearchviewmodel.
<controls:pagingdatagrid itemssource="{binding datacontext.griditems, relativesource={relativesource mode=findancestor, ancestortype=usercontrol}}"/>
or can give x:name
customersearchcontrol
, bind using elementname
.
Comments
Post a Comment