c# - Binding using ValueConverter - ConvertBack Parameter always null -
i have control bound question, question has many possible options. each option can have response.
example:
question: fit in shoe box?
options: yes, no, maybe
response:
options[0].response = new response()
- meaning first option has response, therefore has been answered...
for scenario, have represented using checkbox. if checked, it's answered, if not, response null. hope makes intention clear.
i having problems binding in can't response option. thought i'd able use convert parameter doesn't seem work either..
this how i'm binding check box:
<checkbox content="{binding text}" ischecked="{binding response, mode=twoway, converter={staticresource isquestionresponsenullconverter}, converterparameter={binding response}}" />
so checkbox value converter, works 1 way using following:
public object convert(object value, type targettype, object parameter, string language) { iresponse response = (iresponse )value; if (response== null) { false; } return true; }
however, want bind using convertback
method if check box selected, binding question's response of checkbox's binding context. thought able using converter parameter shown in xaml above, seems parameter null, doing wrong?
i have read on , sources saying dependencyobject required, don't understand why however, surely that's convertback for?
public object convertback(object value, type targettype, object parameter, string language) { if ((bool)value == true) { return parameter; //<-- null.. } else { return null; } }
the converter should be:
public class isquestionresponsenullconverter : dependencyobject, ivalueconverter { public object parameter { { return (object)getvalue(parameterproperty); } set { setvalue(parameterproperty, value); } } public static readonly dependencyproperty parameterproperty = dependencyproperty.register("parameter", typeof(object), typeof(isquestionresponsenullconverter), new propertymetadata(null)); public object convert(object value, type targettype, object parameter, string language) { iresponse response = (iresponse)value; if (response == null) { return false; } return true; } public object convertback(object value, type targettype, object parameter, string language) { if ((bool)value == true) { return parameter; //get parameter } else { return null; } } }
and in xaml
<page.resources> <local:isquestionresponsenullconverter x:key="isquestionresponsenullconverter" parameter="{binding response}"/> </page.resources> <grid x:name="grid_master" background="red"> <checkbox verticalalignment="top" content="{binding text}" ischecked="{binding response, mode=twoway, converter={staticresource isquestionresponsenullconverter}}"/> </grid>
and in method parameter !
i hope find easy use , useful
Comments
Post a Comment