c# - Dynamic Grid Measurements in WPF - Rendering Controls -


i working on drag , drop editor creating items , adding them dynamically wpf canvas. each item, creating dynamic grid , addign canvas. need layout state information each 1 of these grids added know coordinates on canvas. problem having when try access height/actualheight/renderedsize information of each of these grids add, turns out 0. assuming might need render items new state information registered, not quite sure how this. have seen support through invalidatexxxx() methods provided, not sure if/or 1 should using. there updatelayout() function, not sure if need.

here code have running through loop , adding grids, represent lineitems in canvas.

                /*initialize grid layout*/                 grid newgrid = new grid();                 newgrid.minheight = 50;                 newgrid.width = previewwindow.actualwidth;                 newgrid.background = brushes.beige;                 newgrid.showgridlines = true;                  /*define column definitions*/                 list<columndefinition> columns = new list<columndefinition>(fielditemlist.count);                 foreach (columndefinition column in columns)                 {                     columndefinition labelcolumn = new columndefinition();                     newgrid.columndefinitions.add(labelcolumn);                     newgrid.columndefinitions.add(column);                 }                  /*define row definitions*/                 rowdefinition row = new rowdefinition();                 newgrid.rowdefinitions.add(row);                  int colcount = 0;                 (int = 0; < fielditemlist.count; i++)                 {                     fielditem fielditem = fielditemlist[i];                      /*initialize example label*/                     label label = new label();                     label.content = fielditem.label;                     grid.setrow(label, 0);                     grid.setcolumn(label, colcount);                     newgrid.children.add(label);                      /*initialize example text box*/                     textbox textbox = new textbox();                     grid.setrow(textbox, 0);                     grid.setcolumn(textbox, colcount + 1);                     newgrid.children.add(textbox);                     colcount++;                 }                  stackpanel.children.add(newgrid);                  //i need access height of each of grids here somehow                 lasttop += (int)newgrid.actualheight ;                 newgrid.invalidatemeasure();                 //or invalidatevisual(), or perform on else?              } 

there reference stackpanel, of these grids being added stack panel, , eventuall arranged coordinates(which cannot grab). problem right adding these items list need sorted top coordinate, since top coordinate 0, not sort correctly.

a few notes:

  1. the stackpanel not provide size layout suggestions children, grid need supply it's own layout size. in order work, you'll need set row , column sizes "auto" expand accommodate contents. then, i'm not entirely sure if works (usually combine auto "*" rows or columns, rather having auto columns, having them expand fixed or stretched overall grid size.) layout i've proposed here behave bit oddly, dynamically resize columns based on contents of column text boxes; instance, if have no text in row, you'll 0 width text boxes margin , border providing size.

  2. once stackpanel has had children added, you'll need call updatelayout on stackpanel. cause compute layout information each of children , position them appropriately. can become expensive if keep adding grids dynamically, fair warning.


Comments

Popular posts from this blog

java - SNMP4J General Variable Binding Error -

sql server - python to mssql encoding problem -

windows - Python Service Installation - "Could not find PythonClass entry" -