c# - Add item to itemssource if no match from binding -


so, here's scenario: have combobox itemssource dictionary<int, string> of various titles , ids. these titles can disabled @ point in future, , therefore shouldn't show in combobox anymore. however, when viewing old item, still need able show old value in addition current active titles. i'll try visualize little better below.

today:

the combobox items consist of

  • 1, title1
  • 2, title2
  • 3, title3

title3 selected , id (3) stored.

tomorrow:

title3 disabled , title4 added, items consist of

  • 1, title1
  • 2, title2
  • 4, title4

however, if our value yesterday value we're binding (id 3), there no matching item. ideally, i'd append our old item end so:

  • 1, title1,
  • 2, title2
  • 4, title4
  • 3, title3

there separate lists enabled , disabled titles, , item doesn't bind reference disabled titles variable.

i have investigated fallbackvalues , prioritybindings can't seem find way make them fit i'm trying do. perhaps sort of converter used fallbackvalue? appreciate , feedback.

also, reference, code i'm working (i'm looking in datagrid).

wpf:

<datagridtemplatecolumn x:name="tktitle" header="title" width="150">     <datagridtemplatecolumn.celltemplate>         <datatemplate>             <combobox itemssource="{binding datacontext.tasktitles, relativesource={relativesource mode=findancestor, ancestortype={x:type datagrid}}}" selectedvalue="{binding path=tkttid, mode=twoway, updatesourcetrigger=propertychanged}" selectedvaluepath="key" displaymemberpath="value" width="auto" />         </datatemplate>     </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> 

associated code in viewmodel:

public dictionary<int, string> tasktitles {         {         return bestclass.tasktitles;     } } 

edit - working code here code used working. itemssource updated multibinding converter. multibinding contains active tasktitles, tasktitles, , id. these passed converter id checked in active list, , added if it's not active. help!

wpf

<datagridtemplatecolumn x:name="tktitle" header="title" width="150">     <datagridtemplatecolumn.celltemplate>         <datatemplate>             <combobox selectedvalue="{binding path=tkttid, mode=twoway, updatesourcetrigger=propertychanged}" selectedvaluepath="key" displaymemberpath="value" width="auto">                 <combobox.itemssource>                     <multibinding converter="{staticresource checkdisabledtasktitle}" mode="oneway">                         <multibinding.bindings>                             <binding path="datacontext.tasktitles" relativesource="{relativesource mode=findancestor, ancestortype={x:type datagrid}}" />                             <binding path="datacontext.alltasktitles" relativesource="{relativesource mode=findancestor, ancestortype={x:type datagrid}}" />                             <binding path="tkttid" />                         </multibinding.bindings>                     </multibinding>                 </combobox.itemssource>             </combobox>         </datatemplate>     </datagridtemplatecolumn.celltemplate> </datagridtemplatecolumn> 

codebehind converter

class checkdisabledtasktitle : imultivalueconverter {     //values[0] - active values     //values[1] - values (including disabled)     //values[2] - current id     public object convert(object[] values, type targettype, object parameter, system.globalization.cultureinfo culture)     {         //check see if active values contain our current id         if (((dictionary<int, string>)values[0]).containskey((int)values[2]))         {             //they do, return active values             return values[0];         }         else         {             //they don't, we'll add our disabled value active list             ((dictionary<int, string>)values[0]).add((int)values[2], ((dictionary<int, string>)values[1])[((int)values[2])]);             //then give             return values[0];         }     }      public object[] convertback(object value, type[] targettypes, object parameter, system.globalization.cultureinfo culture)     {         throw new notimplementedexception();     } } 

if understand you're after here...

1) i believe should modify things in view-model - , supply default if result null or empty (you need implement inotifypropertychanged notify of changes there etc.).

2) the other option sort of converter - takes list property , use multibinding.

for details see post of mine (mostly code there) - refreshing value converter on inotifypropertychanged

basically bind both proper property list - , 'default list' - , inside converter can make decisions do.

e.g.:

<combobox  ...>     <combobox.itemssource>         <multibinding converter="{staticresource imageconverter}" mode="oneway">             <multibinding.bindings>                 <binding path="tasktitles" />                 <binding path="fallbacktitles" />                 <!--binding path="" /-->             </multibinding.bindings>         </multibinding>     </combobox.itemssource> </combobox> 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -