c# - wpf - Data Template, change template with button -
my task make datatemplate list, , create button changing view. have "data" , "footballteam" classes, , have static resources. need button event, how can change current template?
as tip, example says use method:
"this.resources[resource-key] data-type;"
the xaml:
<window x:class="wpfapplication11.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="250" width="300"> <window.resources> <datatemplate x:key="teamname"> <textblock fontweight="bold" text="{binding path=teamname}"></textblock> </datatemplate> <datatemplate x:key="year"> <textblock text="{binding path=foundingyear}"></textblock> </datatemplate> <datatemplate x:key="logo"> <image source="{binding path=image}" /> </datatemplate> </window.resources> <grid> <grid.rowdefinitions> <rowdefinition height="*" /> <rowdefinition height="auto" /> </grid.rowdefinitions> <scrollviewer grid.row="0" allowdrop="true"> <listbox name="lstteams"> </listbox> </scrollviewer> <button grid.row="1" margin="6">change view</button> </grid> </window>
i guess want change listbox template so, try :
in xaml
<button grid.row="1" margin="6" click="changetemplate">change view</button>
in c#
lstteams.itemtemplate = (datatemplate)this.resources["teamname"];
you have handle different templates want cycle through but, pretty how code-behind.
Comments
Post a Comment