c# - Dynamic column binding -


how can make dynamic set columns in grid or grid analog should use?

i have list, example list<list<string>> = {{a},{b,c},{d,e},{f,g,h}} , need make each element row, , sub-elements should stretched. on picture below. if there 1 element, takes grid width, if there 2 sub-elements each takes half of grid's width , on. got xaml here here is

<page>    <page.resources>         <datatemplate x:key="datatemplate_level2">                 <button  content="{binding}" margin="1,1,1,1" verticalalignment="stretch" horizontalalignment="stretch"/>         </datatemplate>          <datatemplate x:key="datatemplate_level1">             <itemscontrol itemssource="{binding}" itemtemplate="{staticresource datatemplate_level2}" horizontalalignment="stretch" verticalalignment="stretch">                 <itemscontrol.itemspanel>                     <itemspaneltemplate>                                 <stackpanel orientation="horizontal"  horizontalalignment="stretch" verticalalignment="stretch"></stackpanel>                     </itemspaneltemplate>                 </itemscontrol.itemspanel>             </itemscontrol>         </datatemplate>     </page.resources>      <grid>         <itemscontrol itemssource="{binding}" x:name="lst" itemtemplate="{staticresource datatemplate_level1}"/>     </grid> </page> 

but can't streach sub-elementslike need, this:

picture

try this:

<itemscontrol horizontalalignment="stretch">   <itemscontrol itemssource="{binding items}">     <itemscontrol.itemtemplate>       <datatemplate>         <itemscontrol itemssource="{binding}" horizontalalignment="stretch">           <itemscontrol.itemtemplate>             <datatemplate>               <button content="{binding}" margin="1,1,1,1" verticalalignment="stretch"                        horizontalalignment="stretch"/>             </datatemplate>           </itemscontrol.itemtemplate>           <itemscontrol.itemspanel>             <itemspaneltemplate>               <uniformgrid rows="1" />             </itemspaneltemplate>           </itemscontrol.itemspanel>         </itemscontrol>       </datatemplate>     </itemscontrol.itemtemplate>    </itemscontrol> </itemscontrol> 

and how looks

result

edit:

since universal app doesn't support uniformgrid, created own panel:

public class uniformgridsingleline : panel {   protected override size measureoverride(size availablesize)   {     foreach (uielement child in children)       child.measure(availablesize);      return new size(double.ispositiveinfinity(availablesize.width) ? 0 : availablesize.width,         double.ispositiveinfinity(availablesize.height) ? children.cast<uielement>().max(x => x.desiredsize.height) : availablesize.height);   }    protected override size arrangeoverride(size finalsize)   {     size cellsize = new size(finalsize.width / children.count, finalsize.height);     int col = 0;     foreach (uielement child in children)     {       child.arrange(new rect(new point(cellsize.width * col, 0), new size(cellsize.width, child.desiredsize.height)));       col++;     }     return finalsize;   } } 

just replace uniformgrid uniformgridsingleline in xaml above (don't forget namespace)


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -