xaml - How to do relativesource mode find ancestor (or equivalent) in UWP -


i trying 1 think should simple (at least in wpf). have page listbox , datatemplate, datatemplate calls user control button in it. nothing fancy, buttons command not part of listboxsource, , can’t find simple way tell button command. here scenario

<page x:class="app1.mainpage"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   xmlns:local="using:app1"> <page.resources>     <datatemplate x:key="mydatatemplate">         <local:mybutton />     </datatemplate> </page.resources> <listbox itemtemplate="{staticresource mydatatemplate}" itemssource="{binding customers}" /> </page>  <usercontrol x:class="app1.mybutton"          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">  <button command="{binding relativesource={relativesource mode=findancestor, ancestortype=usercontrol, ancestorlevel=2}, path=datacontext.deletecommand}" content="delete" /> </usercontrol> 

please note not compile in uwp there no mode find ancestor? how should it, keep looking @ google can’t find it.

thank you

the answer dependency property. have had same issue. first if have no datatemplate involved solution straight forward:

(this.content frameworkelement).datacontext = this; 

you set datacontext of usercontrol in constructor code behind.

if planning command inside datatemplate need dependecyproperty too.

example:

 <datatemplate>      <button command="{binding datacontext.mycommand, elementname=parentname}">  </datatemplate> 

and create dependency property command:

 public icommand mycommand     {         { return (icommand)getvalue(mycommandproperty); }         set { setvalue(mycommandproperty, value); }     }      // using dependencyproperty backing store mycommand.  enables animation, styling, binding, etc...     public static readonly dependencyproperty mycommandproperty =         dependencyproperty.register("mycommand", typeof(icommand), typeof(ownerclass), new propertymetadata(0)); 

so when use user control have mycommand property on can bind command viewmodel long templating parent matches 1 provided , parameter bound actual item control part of.

<usercontrols:button mycommand="{binding mycommandfromviewmodel}" commandparameter="{binding}"/> 

simple example:

usercontrol code behind

 public sealed partial class listviewusercontrol : usercontrol {     public listviewusercontrol()     {         this.initializecomponent();          (this.content frameworkelement).datacontext = this;     }         public icommand buttoncommand     {         { return (icommand)getvalue(buttoncommandproperty); }         set { setvalue(buttoncommandproperty, value); }     }      // using dependencyproperty backing store buttoncommand.  enables animation, styling, binding, etc...     public static readonly dependencyproperty buttoncommandproperty =         dependencyproperty.register("buttoncommand", typeof(icommand), typeof(listviewusercontrol), new propertymetadata(null));         public observablecollection<item> itemssource     {         { return (observablecollection<item>)getvalue(itemssourceproperty); }         set { setvalue(itemssourceproperty, value); }     }      // using dependencyproperty backing store itemssource.  enables animation, styling, binding, etc...     public static readonly dependencyproperty itemssourceproperty =         dependencyproperty.register("itemssource", typeof(observablecollection<item>), typeof(listviewusercontrol), new propertymetadata(new observablecollection<item>()));    } 

usercontrol xaml:

<grid>     <listview itemssource="{binding itemsource}" x:name="listview">         <listview.itemtemplate>             <datatemplate>                 <!--some item related content-->                 <appbarbutton icon="delete" command="{binding buttoncommand, elementname=listview}" commandparameter="{binding}"/>             </datatemplate>         </listview.itemtemplate>     </listview> </grid> 

usage in page.xaml:

<controls:listviewusercontrol itemssource="{binding viewmodelsitemslist}" buttoncommand="{binding viewmodelscommand}"/> 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -