WPF: add several objects into my ListView - only the first one is added -


i have object want add listview:

public class mydata {     string name {get; set;} } 

this how try add object behind code:

observablecollection<mydata> collection = new observablecollection<mydata>();             mydata data1 = new mydata("c:\file.doc");             collection.add(data1);             mylistview.items.add(collection); 

this works fine if want add one:

observablecollection<mydata> collection = new observablecollection<mydata>();             mydata data1 = new mydata("c:\file.doc");             collection.add(data1);              mydata data2 = new mydata("c:\blabla.doc");             collection.add(data2);              mylistview.items.add(collection); 

i can see first object.

listview xaml:

    <listview name="mylistview" margin="16,453,263,40" background="transparent" borderthickness="0" >         <listview.itemcontainerstyle>             <style targettype="{x:type listviewitem}">                 <setter property="foreground" value="white"/>             </style>         </listview.itemcontainerstyle>         <listview.resources>             <datatemplate x:key="mydatatemplate">                 <grid margin="-6">                     <progressbar maximum="100" value="{binding progress}"                                   width="{binding path=width, elementname=progresscell}" height="16" margin="0"                                  foreground="#ff5591e8" />                     <label content="{binding progress, stringformat={}{0}%}" fontsize="12"                            verticalalignment="center" horizontalalignment="center" />                 </grid>             </datatemplate>             <controltemplate x:key="progressbartemplate">                 <label  horizontalalignment="center" verticalalignment="center" />             </controltemplate>         </listview.resources>         <listview.view>             <gridview columnheadercontainerstyle="{staticresource listviewheaderstyle}">                 <gridviewcolumn width="425" header="file name" displaymemberbinding="{binding filename}" />                 <gridviewcolumn x:name="progresscell"  width="50" header="progress"                                 celltemplate="{staticresource mydatatemplate}" />             </gridview>         </listview.view>     </listview> 

define collection follows :

public observablecollection<data> datalist { get; set; } 

initialize in constructor , add elements according needs. set data context of window :

this.datacontext = this; 

then data bind in xaml :

<listview name="mylistview"                            itemssource="{binding datalist}"/> 

it should work , should see both elements added.

for specific case, don't add collection items, set itemssource of listview.

mylistview.itemssource = collection; 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -