XAML C# Tag to Double -


i have xaml file bunch of textblocks in them, , button tag contains average of values. code button this:

<button x:name="ltavg_button" cursor="hand" grid.row="1" grid.column="3" fontsize="20"           width="230"           content="&lt; average systolic"           tag="116.21428571428571"           click="ltavg_button_click"/> 

the code supposed change foreground of textblock grey, , takes value button's tag, , value textblocks (which in list), , compares them each other. problem tag being converted weird value doesn't make sense.

the listener code, in c#:

 private void gtavg_button_click(object sender, routedeventargs e)  {    double avg = double.parse(ltavg_button.tag.tostring());    foreach (textblock tb in dia)    {      int txt = int.parse(tb.text);      if (txt < avg)      {        tb.foreground = new solidcolorbrush(darkslategray);      }    }  } 

because avg value weird, considers condition true if shouldn't be. values 110 or less, others higher 120

any appreciated, i've been pulling hair out on long time.

ok. delete code , start over.

first of all, have serious misconception here: the ui not right place store data.

therefore, should not placing numeric values in xaml, instead should create proper viewmodel store these numbers , operate on them:

<window x:class="miscsamples.averagenumberssample"         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"         title="averagenumberssample" height="300" width="300">     <dockpanel>         <button content="calculate" click="calculate" dockpanel.dock="top"/>         <listbox itemssource="{binding}">             <listbox.itemtemplate>                 <datatemplate>                     <textblock text="{binding value}" x:name="txt"/>                     <datatemplate.triggers>                         <datatrigger binding="{binding isbelowaverage}" value="true">                             <setter targetname="txt" property="foreground" value="blue"/>                         </datatrigger>                     </datatemplate.triggers>                 </datatemplate>             </listbox.itemtemplate>         </listbox>     </dockpanel> </window> 

code behind:

public partial class averagenumberssample : window {     public double average = 116.21428571428571;     public list<averagesampleviewmodel> values { get; set; }       public averagenumberssample()     {         initializecomponent();         datacontext = values = enumerable.range(100, 150)                                         .select(x => new averagesampleviewmodel() { value = x })                                         .tolist();     }      private void calculate(object sender, routedeventargs e)     {         values.foreach(x => x.isbelowaverage = x.value < average);     } } 

data item:

public class averagesampleviewmodel: propertychangedbase {     public int value { get; set; }      private bool _isbelowaverage;     public bool isbelowaverage     {         { return _isbelowaverage; }         set         {             _isbelowaverage = value;             onpropertychanged("isbelowaverage");         }     } } 

propertychangedbase class (mvvm helper)

public class propertychangedbase:inotifypropertychanged {     public event propertychangedeventhandler propertychanged;      protected virtual void onpropertychanged(string propertyname)     {         application.current.dispatcher.begininvoke((action) (() =>                                                                  {                                                                      propertychangedeventhandler handler = propertychanged;                                                                      if (handler != null) handler(this, new propertychangedeventargs(propertyname));                                                                  }));     } } 

result:

enter image description here

  • in sense, wpf fundamentally different else in existence (such multiple dinosaur archaic ui frameworks available java or stuff that).
  • you must never use ui store data, instead use ui show data. these concepts fundamentally different.
  • my example (which right way wpf applications), removes need int.parse() or casting stuff that, because viewmodel has proper data type required.

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -