c# - WPF - adding item of different template in listview -
c# - WPF - adding item of different template in listview -
when item on listview clicked want create 1 of different template appear below it. have defined templates in window.resources , thought changing itemtemplate when item clicked, adding new item , changing default template, using list listview itemssource showed 1 template , because (for now) both of them same in bindings. should do?
code:
public partial class mainwindow : window { public mainwindow() { initializecomponent(); list<transactionitem> item = new list<transactionitem>(); item.add(new transactionitem() { category="deska", index="2", name="topielec"}); list<object> transactions = new list<object>(); transactions.add(new transaction() { name = "maciek", surname = "chludzinski", begin = "dzisiaj 20:14", end = "dzisiaj 21:14", cost = "240 zł", remain = "42 minuty", items = item }); obmainlistbinding.itemtemplate = (datatemplate)this.findresource("lessclienttemplate"); messagebox.show(obmainlistbinding.itemtemplate.datatemplatekey.tostring()); transactions.insert(1, new transaction() { name = "jhadf", surname = "chludzhadfi", begin = "dhad:14", end = "dzisiajah", cost = "240 zł", remain = "42 minuty"}); obmainlistbinding.itemtemplate = (datatemplate)this.findresource("basictransactiontemplate"); obmainlistbinding.itemssource = transactions; } public class transaction { public string name { get; set; } public string surname { get; set; } public string begin { get; set; } public string end { get; set; } public string remain { get; set; } public string cost { get; set; } public list<transactionitem> items { get; set; } } public class transactionitem { public string category { get; set; } public string index { get; set; } public string name { get;set; } } public class lesstransaction { public string name { get; set; } public string surname { get; set; } public string begin { get; set; } public string end { get; set; } public string remain { get; set; } public string cost { get; set; } } } xaml:
<window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:ignorable="d" x:class="surfmanager.mainwindow" title="mainwindow" height="524.5" width="1078"> <window.triggers> <eventtrigger routedevent="frameworkelement.loaded"/> </window.triggers> <window.resources> <datatemplate x:key="basictransactiontemplate"> <grid maxheight="50"> <grid.rowdefinitions> <rowdefinition height="41*"/> <rowdefinition height="42*"/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="0*"/> <columndefinition width="192*"/> <columndefinition width="234*"/> <columndefinition width="189*"/> <columndefinition width="443*"/> </grid.columndefinitions> <label name="name" content="{binding name}" grid.rowspan="1" grid.column="1" grid.columnspan="1" /> <label name="surname" content="{binding surname}" grid.row="1" grid.column="1" grid.columnspan="1"/> <label name="begin" content="{binding begin}" grid.column="2"/> <label name="end" content="{binding end}" grid.row="1" grid.column="2"/> <label name="remain" content="{binding remain}" grid.column="3"/> <label name="price" content="{binding price}" grid.row="1" grid.column="3"/> <listview name="lvitems" itemssource="{binding items}" grid.column="4" grid.rowspan="2"> <listview.itemtemplate> <datatemplate> <stackpanel orientation="horizontal"> <textblock text="{binding category}" /> <textblock text=", nr" /> <textblock text="{binding index}" /> <textblock text="{binding name}" /> </stackpanel> </datatemplate> </listview.itemtemplate> </listview> </grid> </datatemplate> <datatemplate x:key="lessclienttemplate"> <grid maxheight="50"> <grid.rowdefinitions> <rowdefinition height="41*"/> <rowdefinition height="42*"/> </grid.rowdefinitions> <grid.columndefinitions> <columndefinition width="0*"/> <columndefinition width="192*"/> <columndefinition width="234*"/> <columndefinition width="189*"/> <columndefinition width="443*"/> </grid.columndefinitions> <label name="name" content="{binding name}" grid.rowspan="1" grid.column="1" grid.columnspan="1" /> <label name="surname" content="{binding surname}" grid.row="1" grid.column="1" grid.columnspan="1"/> <label name="begin" content="{binding begin}" grid.column="2"/> <button name="end" content="{binding end}" grid.row="1" grid.column="2"/> </grid> </datatemplate> </window.resources> <grid> <tabcontrol> <tabitem header="obecne wypożyczenia" background="#fff40aff" foreground="black" borderbrush="#ff8c8e94" opacitymask="white"> <grid background="#ffe5e5e5"> <grid.columndefinitions> <columndefinition/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition/> </grid.rowdefinitions> <listview name="obmainlistbinding" borderthickness="0" itemtemplate="{staticresource basictransactiontemplate}"> <listview.resources> <style targettype="listviewitem"> <setter property="template"> <setter.value> <controltemplate targettype="listviewitem"> <border background="{templatebinding background}"> <contentpresenter /> </border> </controltemplate> </setter.value> </setter> </style> </listview.resources> </listview> </grid> </tabitem> <tabitem header="baza klientow"> <grid background="#ffe5e5e5"> <listview borderthickness="0" > <listviewitem height="66" background="red"> <grid background="black" > <grid.columndefinitions> <columndefinition width="0*"/> <columndefinition width="0*"/> <columndefinition width="0*"/> </grid.columndefinitions> <grid.rowdefinitions> <rowdefinition height="0*"/> <rowdefinition height="0*"/> <rowdefinition height="0*"/> </grid.rowdefinitions> </grid> </listviewitem> </listview> </grid> </tabitem> <tabitem header="tabitem" horizontalalignment="left" height="35.293" verticalalignment="top" width="57.32" margin="-2,-2,0,-13.333"> <button content="button" height="100" margin="430,174,430,173"/> </tabitem> </tabcontrol>
you seek utilize itemtemplateselector property:
how utilize itemtemplateselector headertemplate in wpf listview?
and(or) triggers alter template:
how alter command template style.triggers
edit
to utilize templateselector add together dummy property on items(or wrap them) allow discern new item old.
... public int32 generation { ... } public class generationtypeselector : datatemplateselector { public override datatemplate selecttemplate(object item, dependencyobject container) { var transaction = (transactionitem)item; if (transaction .generation == 0) homecoming gen0template; else homecoming gen1template; } } c# wpf listview itemtemplate
Comments
Post a Comment