xaml - WPF binding - DataGrid.Items.Count -


in view, there datagrid , textbox, bound datagrid's items.count property:

<datagrid x:name="datagrid" itemssource="{binding datatable}"/> <textbox text="{binding items.count,elementname=datagrid,mode=oneway,stringformat={}{0:#}}"/> 

the viewmodel has property (e.g. itemscount) i'd bound items.count property of datagrid, have no idea, how achieve this.

class viewmodel : inotifypropertychanged {     public datatable datatable {get;set;}     public int itemscount {get;set;} } 

maybe use rows.count property of datatable datagrid bound to, how bind or link 2 properties in viewmodel?

so want itemscount property synchronized datatable.rows.count property.

a common way achieve requirements declare properties data bind ui controls:

<datagrid x:name="datagrid" itemssource="{binding items}" /> <textbox text="{binding itemscount}" /> 

...

// need implement inotifypropertychanged interface here  private observablecollection<yourdatatype> items = new observablecollection<yourdatatype>(); public observablecollection<yourdatatype> items {     { return items; }     set { items = value; notifypropertychanged("items"); notifypropertychanged("itemcount"); } }  public string itemcount {     { items.count.tostring("{0:#}"); } } 

update >>>

as @sivasubramanian has added own requirement question, in case need update item count adding collection, can manually call notifypropertychanged method:

items.add(new yourdatatype()); notifypropertychanged("itemcount"); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -