c# - Using Binding converter to display and update a property in silverlight -
i using converter class display property called noofweeks based on normal working hours(40), example if value stored in database 40 displaying 1 in datagrid column, if 80 displaying 2, here xaml , converter code
<sdk:datagridtemplatecolumn width="*" canuserreorder="false" headerstyle="{staticresource datagridbaseheaderstyle}" header="fte"> <sdk:datagridtemplatecolumn.celltemplate> <datatemplate> <grid> <textblock text="{binding hours, converter={staticresource fteconverter}}" verticalalignment="stretch" margin="4,2" horizontalalignment="stretch"></textblock> </grid> </datatemplate> </sdk:datagridtemplatecolumn.celltemplate> <sdk:datagridtemplatecolumn.celleditingtemplate> <datatemplate> <textbox text="{binding hours,mode=twoway, converter={staticresource fteconverter}}" verticalalignment="top" margin="4,2" maxlength="50" horizontalalignment="stretch" > </textbox> </datatemplate> </sdk:datagridtemplatecolumn.celleditingtemplate> </sdk:datagridtemplatecolumn> public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value == null) return false; else { // assumes rowtype has been passed bound value double hours= (double)value; return math.round(hours/40,0); } }
so data grid userid 1 , 2 no of hours in database 80 , 120 respectively looks like
userid noofweeks standardhours(not visible user) 1 2 80 2 3 120
this working fine expected, want update hours in database based on noofweeks modifed user.
for example in above table if user userid updates noofweeks 4 want save hours 160 (4 *40).
userid noofweeks standardhours(not visible user) 1 4 160 2 3 120 getting bit confused here how update property?
for new mvvm , converters, did use convertback method convert user input hours
public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) { if (value == null) return false; else { // assumes rowtype has been passed bound value string val = (string)value; double doublevalue = double.tryparse(val, out doublevalue) ? doublevalue : 0; return doublevalue * 40; } } }
one thing note if user enters text or besides numbers converting value 0. might not suit requirements , can use idataerrorinfo in property setters stop , notify users entering values not allowed.
Comments
Post a Comment