c# - Binding SelectedValue through converter not matching anything in ItemsSource -
i've got combobox looks following, selectedvalue
bound int
, itemssource
bound collection of string
s:
<combobox selectedvalue="{binding value, converter={staticresource priorityint2stringconverter}}" itemssource="{binding path=stringtointdictionary.keys, source={x:static helpers:helperclass.instance}}"/>
the priorityint2stringconverter
converter looks this:
public class priorityinttostringconverter : ivalueconverter { private static readonly helperclass helper = helperclass.instance; public object convert(object value, type targettype, object parameter, cultureinfo culture) { if (value int) { int priority = (int)value; string prioritystr; if (helper.inttostringdictionary.trygetvalue(priority, out prioritystr)) { value = prioritystr; } } return value; } public object convertback(object value, type targettype, object parameter, cultureinfo culture) { string priority = value string; if (priority != null) { int priorityint; if (helper.stringtointdictionary.trygetvalue(priority, out priorityint)) { value = priorityint; } } return value; } }
the combobox options populating expect, , when converter called, returns expected, called when control first rendered -- never on selection change when have expected.
now, problem: when combobox selection changed, change not stick -- setter on value
never called , selection lost.
your binding looks ok, when selection changes, convertback
method of converter should called. have checked that?
it's important returned value
of convertback
method returns integer. otherwise value
property binded cannot set properly.
if if
-statement:
if (helper.stringtointdictionary.trygetvalue(priority, out priorityint))
returns false
, value
never gets set integer , setter of value
property never gets called. error, can seen in output-window when debugging (probably formatexception
).
so start investigating @ convertback
method. make sure conversion string
int
works in every case. maybe should specify default value, returned if trygetvalue
method returns false
make sure valid value returned in every case.
Comments
Post a Comment