c# - How to bind combo box to another value than the actual by offsetting it? -


i have combo box marked follows.

<combobox selectedindex="{binding path=bonkey}">   <comboboxitem content="monkey" />   <comboboxitem content="donkey" /> </combobox> 

i'm binding object has bonkey field types integer, according declaration below.

class thingy {   public int bonkey {get; set; }   ... } 

although works great , supposed to, there's programmatically technical issue keeping me @ night. indexes generated in manual mark 0 , 1. however, know values of integers 1 , 2. (i.e. monkey indexed 0 when related combo box item it's actual value in object used data source 1. analogously, monkey has index 1 in items of combo box corresponds 2 in object.)

my intermediate solution knock off 1 in constructor right before setting data context, followed kick 1 when view being disposed. it's working can't proud, speak.

public somedialog(thingy thingy) {   initializecomponent();   thingy.bonkey--;   datacontext = thingy; } ... private void cancel_click(object sender, routedeventargs eventargs) {   dialogresult = false;   datacontext.bonkey++;   close(); } ... private void submit_click(object sender, routedeventargs eventargs) {   datacontext.bonkey++;   ... } 

how can more, well... unshamely?

  1. i couldn't locate attributes explicitly set indexes of items in combo box.
  2. i've tried using converters somehow, either got empty stuff in combo box (no pre-selection) or weird error message telling me made boo-boo (and since wasn't sure if approach let go).
  3. googling gave bunch of results, none of gave me clarity of how offset value bound (although honest, suspect might using weird key words, because seems such issue must've been discussed before).

there number of questions involving offsets , ivalueconverter implementations. browsing through them, don't see 1 addresses specific scenario of binding offset; many of questions involve people have converter working having other problems, , others involve scenarios in 1 way or more complex one.

so, here's simple offset converter implementation:

class offsetvalueconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         int offset = int.parse((string)parameter);          return (int)value - offset;     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         int offset = int.parse((string)parameter);          return (int)value + offset;     } } 

used this:

<combobox selectedindex="{binding offsetvalue,                           converter={staticresource offsetvalueconverter1},                           converterparameter=1}"/> 

where, of course, have resource declared make instance of converter available, e.g.:

<window.resources>   <l:offsetvalueconverter x:key="offsetvalueconverter1"/> </window.resources> 

there other options implementation, such giving converter instance property set control offset, or specifying offset actual int value didn't have parsed, these approaches have own limitations, such not being able reuse same instance different offsets, or requiring more verbose declaration in xaml, respectively. think above strikes balance between convenience , efficiency.


see related questions:
how can bind 1 property property, offset specific amount?
applying transforms datatemplates in wpf

there others, these seem closely related own scenario.


Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -