c# - Changing Textblock background in WPF resetting itself after color change -


i have textblock in wpf usercontrol defined as:

<textblock grid.column="0"            text="{binding recognitionresults}"            background="{binding resultbackground}" /> 

the usercontrol in textblock displayed being presented string usercontrol as:

<itemscontrol itemssource="{binding strings}" >       <itemscontrol.itemcontainerstyle>            <style>                 <setter property="control.margin"                         value="{binding margin}"/>            </style>       </itemscontrol.itemcontainerstyle>  </itemscontrol>   

essentially, itemscontrol presenting list of "strings", each string being represented own usercontrol.

now, when tap on display of textblock, gesture performs action change background color yellow green in viewmodel:

public void refill() {     resultbackground = brushes.green; } 

the resultbackground color defined in viewmodel as:

 private solidcolorbrush resultbackground =      (solidcolorbrush)new brushconverter().convertfromstring("#ffeff100");   public solidcolorbrush resultbackground  {             {            console.writeline("now getting resultbackground of "                + resultbackground);            return resultbackground;       }       set       {           if (resultbackground != value)           {               resultbackground = value;               onpropertychanged("resultbackground");           }       }  } 

so, when physically tap on textblock, gesture turn yellow green. far good.

however, when execute refill() method command (i.e., menu command), textblock first turns green (like should) redisplays initial yellow. watching output console.writeline above confirms first called turn box green refill(), (there no stack trace), called again (without set being called) retrieves yellow!

i @ complete loss why happening or fix it. places in code resultbackground referenced in above code.

any in getting work appreciated.

edit: don't know if relevant or not, command executes refill() being executed action menu as:

 <usercontrol x:class="nova5.ui.views.ink.inkview"         ............................  <usercontrol.resources>     <style x:key="menuitemstyle" targettype="{x:type menuitem}">          <setter property="command" value="{binding onselected}" />     </style> </usercontrol.resources>   <grid>     <grid.resources>         <hierarchicaldatatemplate datatype="{x:type m:mymenuitem}" itemssource="{binding path=subitems}">             <contentpresenter                 content="{binding path=displaytext}"                 recognizesaccesskey="true" />         </hierarchicaldatatemplate>     </grid.resources>      <grid.rowdefinitions>         <rowdefinition height="auto"/>         <rowdefinition height="auto"/>         <rowdefinition height="*"/>         <rowdefinition height="25" />     </grid.rowdefinitions>      <menu grid.row="3" height="28" >         <menuitem header="options" itemssource="{binding optionsubitems}" displaymemberpath="{binding displaytext}" >             <menuitem.itemcontainerstyle>                 <style>                     <setter property="menuitem.command"  value="{binding onselected}"/>                 </style>             </menuitem.itemcontainerstyle>         </menuitem>     </menu>  </grid> 

where optionsubitems built in viewmodel constructor as:

  optionsubitems = new observablecollection<mymenuitem>(); 

and mymenuitem is:

   public class mymenuitem : menuitembase {     private action command;      public mymenuitem(string displaytext, action command)     {         this.displaytext = displaytext;          this.command = command;     }     public override void onitemselected()     {         this.command();     } } 

the viewmodel constructor dynamically builds command list as:

    optionsubitems.add(new mymenuitem("refill", delegate()         {             currentviewmodeldetails.executemenucommand("refill");         })); 

and executemenucommand does:

     if (commandname == "refillallcurrentprescriptions")         {              (int k = 0; k < strings.count; k++)             {                 strings[k].refill();             }         } 

hope helps. (i wondering if problem in above xaml 2 bindings onselected different styles?)

o.k., @ risk of total embarrassment, here original code causing blinking problem:

 optionsubitems.add(new mymenuitem("refill current prescriptions", delegate()         {             currentviewmodeldetails.executemenucommand("refillallcurrentprescriptions");              currentviewmodeldetails.refreshdisplay(rxviews.prescriptionlist);             dialogtitle = "current prescriptions";         })); 

the refreshdisplay() method rebuilding display database , totally dropping effects of refill()--which explains why breakpoints placed on get{} did not show stack trace. get{} being called xaml when new objects created; there no set{} involved. above code works fine (after removed offending line more concise so).

to give credit credit due, rachel's suggestion led restudying delegates, help.


Popular posts from this blog