wpf - Add controls to empty cells in a Grid that's the ItemsPanelTemplate for a ListView -


i have wpf listview, has grid itemspaneltemplate. display items in correct column , row based on property of item. put controls in empty cells of grid.

this simplified version of code , xaml:

in resources:

<itemspaneltemplate x:key="thetemplate">     <grid>         <grid.columndefinitions>             <columndefinition width="1*" />             <columndefinition width="1*" />         </grid.columndefinitions>         <grid.rowdefinitions>             <rowdefinition height="1*"/>             <rowdefinition height="1*"/>         </grid.rowdefinitions>     </grid> </itemspaneltemplate> 

in xaml:

<controls:customlistview itemssource="{binding thecollection}"                             itemspanel="{dynamicresource thetemplate}"> </controls:customlistview> 

finally, in customlistview:

protected override void preparecontainerforitemoverride(dependencyobject element, object item) {     base.preparecontainerforitemoverride(element, item);     var viewmodel = item domainobject;     if (viewmodel != null)     {         element.setvalue(grid.columnproperty, 1); //here work converter, simplifies stackoverflow         element.setvalue(grid.rowproperty, 1);     } } 

note: know i'm casting domainobject, bear me, please.

what give me, grid items in correct row , column. if want display in empty cells, example text 'null'?

i can't add template, because crashes application, saying itemscontrol create necessary controls. i've tried accessing grid/template in code-behind, can't quite find how to. maybe shouldn't using listview? maybe there other/better solutions?

what ended doing was:

  • make grid columndefinitions , rowdefinitions (i know how many need)
  • in code-behind of view, cast datacontext know be
  • i iterate on collection, , each item, create new control , set datacontext item
  • i set rowproperty , columnproperty , add control grid
  • i remember added these controls, can add empty controls grid there aren't yet.

this think change performance reasons: - instead of iteration on collection of items, iterate on possible cells of grid, add control (always), , set datacontext if there corresponding item.

the reason performance. takes 2 secondes fill grid, , i'd faster.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -