wpf - How to calculate percentage value in XAML -


i have circle progress-bar , simple label inside circle

<designincontrol:circularprogressbar x:name="circularprogressbar" horizontalalignment="center" verticalalignment="center"      percentage="0"  />  <label name="lblprogress" content="0%" foreground="white"  grid.column="1" /> 

i have 2 static field:

  1. in main form class - totalfiles
  2. as class member:

    public class data { public static int numberoffiles; }

so in circle , label want show percentage calculate: (data.numberoffiles\totalfiles) * 100

so how can via xaml ?

edit

add new class hold form variable:

public class totalpacketinlist {     private static int _value;      public static int value     {         { return _value; }         set { _value = value; }     } } 

my doubletopercentageconverter class:

public class doubletopercentageconverter : ivalueconverter {     public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         return string.format("{0%}", (int)(((double)data.numberoffiles/ totalfilesinlist.value) * 100));     }      public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture)     {         return 0;     } }  <label name="lblcircleprogress" content="{binding elementname=circularprogressbar,path=percentage, converter={staticresource doubletopercentage}}"        grid.column="1" /> 

this calculate percentage when try run application have exception in label:

a first chance exception of type 'system.windows.markup.xamlparseexception' occurred in presentationframework.dll

additional information: 'set property 'system.windows.controls.contentcontrol.content' threw exception.' line number '481' , line position '45'.

this because when application start value totalpacketinlist.value 0 , calculation divided zero.

you can use converters. in order create converter:

  1. create folder named "converter" , add new class inside i.e: doubletopercentageconverter.cs
  2. this class must implement interface ivalueconverter.

    public class doubletopercentageconverter: ivalueconverter {   public object convert(object value, type targettype, object parameter, string language)   {      //don't know if double cast neccessary      return string.format("{0%}", ((double)value).tostring()));   }    public object convertback(object value, type targettype, object parameter, string language)   {     throw new notimplementedexception();   } } 
  3. add logic inside convert method , return value.. example:

    return value.tostring() + "%";

  4. inside xaml add in resources

    <converter:doubletopercentageconverter x:key="doubletopercentage"/>

    where converter folder in created converter

  5. bind converter element:

    <label name="lblprogress" content="{binding elementname=circularprogressbar,path=percentage, converter={staticresource doubletopercentage}}" foreground="white" grid.column="1" />

  6. in timer_tick event set percentage value of progress bar calculeted value:

    circularprogressbar.percentage = (int)(((double)data.numberoffiles/ totalfilesinlist.value) * 100)) 

let me know if works!


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -