Force Update of RelativeSource Binding in WPF -


for application, have itemscontrol has distinct template (shorthanded template2) first item, , default template (template1) rest of items. have done following xaml:

<datatemplate x:key="mydatatemplate">     <contentpresenter x:name="templatecontentpresenter"                       content="{binding relativesource={relativesource templatedparent}, path=content}"                       contenttemplate="{staticresource template1}"/>     <datatemplate.triggers>          <datatrigger binding="{binding relativesource={relativesource self},       converter= {staticresource isfirstitemincollectionconverter}}"                      value="true">             <setter targetname="templatecontentpresenter"                     property="contenttemplate"                     value="{staticresource template2}"/>         </datatrigger>     </datatemplate.triggers> </datatemplate> 

the isfirstitemincollectionconverter's code follows:

public class isfirstitemincollectionconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, cultureinfo culture)     {         dependencyobject item = (dependencyobject)value;         itemscontrol ic = itemscontrol.itemscontrolfromitemcontainer(item);         console.writeline("converted");         return (ic.itemcontainergenerator.indexfromcontainer(item) == 0);     }      public object convertback(object value, type targettype, object parameter, cultureinfo culture)     {         throw new notimplementedexception();     } } 

the problem binding not update when make change in itemssource of itemscontrol. (in view displays itemscontrol, have buttons can add , remove items). binding updates , shows correct template after switch view, , reload it. how can correct behavior? i'm assuming have set updatesourcetrigger explicit , update source manually, have no idea how inside datatemplate. in advance help.

you have triggered datatrigger on binding="{binding relativesource={relativesource self} triggered when listboxitem's datacontext change.

i suggest here use datatemplateselector item control , set itemtemplateselector property it. in selecttemplate method can return template newly added item depending on logic:

templateselector:

 public class mytemplateselector : datatemplateselector {     public datatemplate firstitemtemplate { get; set; }     public datatemplate defaultitemtemplate { get; set; }      public override datatemplate selecttemplate(object item, dependencyobject container)     {         //logic return first or default template comes here     } } 

in xaml:

<local:mytemplateselector x:key="mytemplate">         <local:mytemplateselector.firstitemtemplate>             <!--first template-->         </local:mytemplateselector.firstitemtemplate>          <local:mytemplateselector.defaultitemtemplate>             <!--default template-->         </local:mytemplateselector.defaultitemtemplate>     </local:mytemplateselector> 

update

for particular case if not using alternation in itemscontrol can solve below also. can use alternationcount listbox , bind itemscontrol.alternationindex each listboxitem. whenever listbox alternation index changes can change template:

xaml:

<listbox alternationcount="{binding items.count, relativesource={relativesource self}}" x:name="itemcontrol" itemssource="{binding items}">         <listbox.itemcontainerstyle>             <style targettype="{x:type listboxitem}">                 <setter property="template" value="{staticresource defaulttemplate}" />                 <style.triggers>                     <trigger property="itemscontrol.alternationindex" value="0">                         <setter property="template" value="{staticresource firsttemplate}" />                     </trigger>                 </style.triggers>             </style>         </listbox.itemcontainerstyle>     </listbox> 

here defaulttemplate , firsttemplate controltemplates.

i tried , worked me whenever remove item @ index 0, gives next item first item template.

hope works you.

thanks,


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -