Monday, 15 September 2014

xaml - WPF Databinding with ObservableCollection to Label -



xaml - WPF Databinding with ObservableCollection to Label -

i have observablecollection need bind 2 labels, first show count of items in collection , sec show sum of values.

first label bound collections count property , sec label bound straight observablecollection convertor calculate total of items

xaml looks this

<grid> <listbox name="itemlist" itemssource="{binding datalist}"/> <label name="lblcount" content="{binding datalist.count}" /> <label name="lbltotal" content="{binding datalist, converter={staticresource calculatetotalconvertor}" /> </grid>

my vm has collection this

observablecollection<int> info = new observablecollection<int>(); public observablecollection<int> datalist { { homecoming data; } set { info = value; } }

my convertor code

public class calculatetotalconvertor : ivalueconverter { public object convert(object value, type targettype, object parameter, cultureinfo culture) { observablecollection<int> collection = value observablecollection<int>; homecoming collection.sum(); } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }

issue on adding new items in datalist, listview , label showing count of items gets updated "lbltotal" doesnt updated total count.

basically how forcefulness binding evaluated on observablecollection changes ? how work straight listview or datagrid not label ?

i know problem can solved creating property in vm show total , raise property alter when collection gets updated there improve solution ?

of-course simplified form of actual problem, dont have access viewmodel , collection, 3rd party control. creating wrapper user command , have relative binding view inner collection.

the other answers correctly explain why not updating. forcefulness update can alter converter imultivalueconverter:

public class calculatetotalconvertor : imultivalueconverter { public object convert(object[] values, type targettype, object parameter, cultureinfo culture) { observablecollection<int> collection = values.firstordefault() observablecollection<int>; homecoming collection.sum(); } public object[] convertback(object value, type[] targettypes, object parameter, cultureinfo culture) { throw new notimplementedexception(); } }

then alter binding multibinding pulls in count:

<label name="lbltotal"> <label.content> <multibinding converter="{staticresource calculatetotalconvertor}"> <binding path="datalist"/> <binding path="datalist.count"/> </multibinding> </label.content> </label>

now sec binding notify binding needs update when items added or removed, can ignore count value , not utilize it.

wpf xaml data-binding

No comments:

Post a Comment