c# - wpf DataGrid of UserControls -


i trying have datagrid shows user controls in each cell of it's rows. highliting datagrid have dynamic because columns count dynamic each case of use.

in xaml code (xaml) have declaration of datagrid :

<grid grid.column="1" margin="0,10,0,0">       <datagrid autogeneratecolumns="false" x:name="planningtable" frozencolumncount="1"/>  </grid> 

my user controle (the usercontrol done , works perfectly): enter image description here

as result of datagrid want have usercontrol in each cell of datagrid means datagrid rows have show usercontrol in each cell. i've searched lot trick seems datagrid can't host usercontrol in cells.

i want have c# code this, please no xaml code because dynamic !!

like mentioned in comment, can dynamically xaml only. doing in code behind, you might end writing lot of code , loose upon important features of wpf. importantly ui virtualization if create rows manually yourself.


in case don't want binding support , want show plain datagrid cells filled usercontrol, can way:

it show 2 columns , 100 rows filled custom user control:

<grid>     <grid.resources>         <objectdataprovider x:key="enumerablerange"                  xmlns:sys="clr-namespace:system;assembly=mscorlib"                  xmlns:linq="clr-namespace:system.linq;assembly=system.core"                  objecttype="{x:type linq:enumerable}" methodname="range">             <objectdataprovider.methodparameters>                 <sys:int32>1</sys:int32>                 <sys:int32>100</sys:int32>             </objectdataprovider.methodparameters>         </objectdataprovider>     </grid.resources>     <datagrid autogeneratecolumns="false" isreadonly="true"             canuseraddrows="false"             canuserdeleterows="false"             itemssource="{binding source={staticresource enumerablerange}}">         <datagrid.columns>             <datagridtemplatecolumn header="test1">                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <local:sampleusercontrol/>                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>             </datagridtemplatecolumn>             <datagridtemplatecolumn header="test2">                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <local:sampleusercontrol/>                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>             </datagridtemplatecolumn>         </datagrid.columns>     </datagrid> </grid> 

update

in case want set columns dynamically, mentioned in comments have set autogeneratecolumns false , manually add columns collection. instead of creating datagridtemplatecolumns manually can declare under resources section of datagrid , use in code behind.

xaml:

<grid>     <grid.resources>         <objectdataprovider x:key="enumerablerange"             xmlns:sys="clr-namespace:system;assembly=mscorlib"             xmlns:linq="clr-namespace:system.linq;assembly=system.core"             objecttype="{x:type linq:enumerable}" methodname="range">             <objectdataprovider.methodparameters>                 <sys:int32>1</sys:int32>                 <sys:int32>100</sys:int32>             </objectdataprovider.methodparameters>         </objectdataprovider>     </grid.resources>     <datagrid autogeneratecolumns="false"                 x:name="datagrid"                 isreadonly="true"                 canuseraddrows="false"                 canuserdeleterows="false"                 itemssource="{binding source={staticresource enumerablerange}}">         <datagrid.resources>             <datagridtemplatecolumn x:key="templatecolumn" x:shared="false">                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <local:sampleusercontrol/>                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>             </datagridtemplatecolumn>         </datagrid.resources>     </datagrid> </grid> 

code behind

public partial class mainwindow : window {     private void createdatagridcolumns()     {         (int = 0; < 10; i++) // change number of columns here.         {             datagridtemplatecolumn templatecolumn =                    (datagridtemplatecolumn)datagrid.resources["templatecolumn"];             templatecolumn.header = string.format("test {0}", + 1);             datagrid.columns.add(templatecolumn);         }     }      public mainwindow()     {         initializecomponent();         createdatagridcolumns();     } } 

Comments

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -