How to dynamically create a grid in WPF? -
i want create grid dynamically show product information e-commerce site.
i fetching product information json , want show each product details tiles view.
instead of using loads of code-behind code, suggestion using itemscontrol wrappanel inside.
<itemscontrol itemssource="{binding products}"> <itemscontrol.itemspanel> <itemspaneltemplate> <wrappanel isitemshost="true" /> </itemspaneltemplate> </itemscontrol.itemspanel> <itemscontrol.itemtemplate> <datatemplate> <!-- item template --> <grid height="120" width="70"> <grid.rowdefinitions> <rowdefinition height="*" /> <rowdefinition height="3*" /> <rowdefinition height="2*" /> </grid.rowdefinitions> <textblock grid.row="0" fontweight="bold" text="{binding productname}" /> <image grid.row="1" source="{binding thumbnail}" /> <textblock grid.row="2" text="{binding description}" /> </grid> <!-- item template --> </datatemplate> </itemscontrol.itemtemplate> </itemscontrol> products collection of items in datacontext (viewmodel, if you're using mvvm), , have bind properties of items in item template (like productname, thumbnail or description in example).
pros:
- less code
- 100% xaml
- adapts available space
- can configured horizontal or vertical, or left-to-right or right-to-left layouts
cons:
- no selection
- no headers
- no editable templates
- mmmh, it's not grid?
if want fancy functionalities, headers, selection, etc., you'll have use datagrid, lose tiled layout.
Comments
Post a Comment