c# - ICommand is going to the itemsource instead of the datacontext -
i know command is, not finding right path.
error: bindingexpression path error: 'onbuttonclickedcommand' property not found on 'app1.helper.foodtypes, app1.windows, version=1.0.0.0, culture=neutral, publickeytoken=null'. bindingexpression: path='onbuttonclickedcommand' dataitem='app1.helper.foodtypes, app1.windows, version=1.0.0.0, culture=neutral, publickeytoken=null'; target element 'windows.ui.xaml.controls.button' (name='button1'); target property 'command' (type 'icommand')
below use element in items source setup tag , content. want command sent, sent datacontext. instead being sent same element.
menuitemspage.xaml
<datatemplate x:key="foodtypetemplate"> <button x:name="button1" width="100" height="50" content="{binding name}" tag="{binding name}" command="{binding onbuttonclickedcommand}" commandparameter="{binding elementname=button1}"/> </datatemplate>
i know command going element of item source (categorybuttonlist) since if replace onbuttonclickedcommand name, path found. instead want going datacontext set in cs file. researched, supposed use relativeresource findancestortype seems not supported on platform.
error 3 property 'ancestortype' not found in type 'relativesource'.
reason why want go datacontext because logic of is.
below how have itemssource setup
menuitemspage.xaml
<listbox horizontalalignment="left" height="425" margin="75,140,0,0" verticalalignment="top" width="105" itemssource="{binding categorybuttonlist}" itemtemplate="{staticresource foodtypetemplate}" > </listbox>
in menuitemspage have datacontext setup in code. perhaps should setting in xaml keep cs clean?
menuitemspage.xaml.cs
public sealed partial class menuitemspage : page { public menuitemspage() { this.initializecomponent(); } protected override void onnavigatedto(navigationeventargs e) { string tablename = e.parameter string; this.datacontext = new menupagevm(tablename); } }
the datacontext
of datatemplate
(and children, such button
) is itemssource
of actual listbox
. if command defined within datacontext
of window, use either elementname
or relativesource
:
<window x:name="mywindow" ... > <listbox itemssource="..."> <listbox.itemtemplate> <datatemplate> <button command="{binding elementname=mywindow, path=datacontext.mycommand}" /> </datatemplate> </listbox.itemtemplate> </listbox> </window>
or
<window ... > <listbox itemssource="..."> <listbox.itemtemplate> <datatemplate> <button command="{binding relativesource={relativesource findancestor, ancestortype={x:type window}}, path=datacontext.mycommand}" /> </datatemplate> </listbox.itemtemplate> </listbox> </window>
Comments
Post a Comment