c# - Bind Dictionary<string, double> entries to a XAML number textblock WPF -


in xaml file, have 3rd party (syncfusion) doubletextbox control:

<syncfusion:doubletextbox x:name="personaheight" style="{staticresource sfdoubletb}" /> 

when doubletextbox loses focus, copies value dictionary have created (the xaml element , dictionary key have exact same name), in order save keys , values use on page.

public static dictionary<string, double> globaldictionary = new dictionary<string, double>() {         {"personaheight", 0}, {"personbheight", 0},         // on 100 keys & values } 

when doubletextbox loses focus (this has been set in styles):

void sftextbox_lostfocus(object sender, routedeventargs e)     {         var tb1 = sender doubletextbox;          if ((double) tb1.value != 0)         {             if (app.globaldictionary.containskey(tb1.name))             {                 app.globaldictionary[tb1.name] = (double) tb1.value;              // replace value                               }             else             {                 app.globaldictionary.add(tb1.name, (double) tb1.value);           // add entry dictionary in app.xaml.cs                              }                        }     } 

all of works expected. able save globaldictionary using json serialization text file, using savefiledialog, , open file again , de-serialize it.

private void saveproject_click(object sender, routedeventargs e) {         savefiledialog savefiledialog = new savefiledialog();          if(savefiledialog.showdialog() == true)         {                                              string jsonstring = app.serializetojsonstring();             file.writealltext(savefiledialog.filename, jsonstring);         } } 

open file:

private void openproject_click(object sender, routedeventargs e) {         openfiledialog openfiledialog = new openfiledialog();         if (openfiledialog.showdialog() == true)         {             string stringtodeserialize = file.readalltext(openfiledialog.filename);             app.deserializejsonstring(stringtodeserialize);             // read data , put value xaml number box         } } 

deserialize json string

public static void deserializejsonstring(string jsonstring)     {         app.globaldictionary = jsonconvert.deserializeobject<dictionary<string, double>>(jsonstring);     } 

all values saved file written globaldictionary.

my question is, after file open , contents read, there easy way put contents number text box through automatically iterating through each xaml element? user should automatically see values have been restored.

the brute force way following after opening file. if possible, i'd prefer not it, since have on 100 elements.

personaheight.value = app.globaldictionary["personaheight"]; ...repeat x 100 

any alternate suggestions appreciated.

why not put 100+ doubletextbox controls inside listbox , define these doubletextbox data template? such :

 <listbox x:name="mylistbox">         <listbox.itemtemplate>             <wrappanel>             <label content={binding key}             <syncfusion:doubletextbox style="{staticresource sfdoubletb}" value={binding value}/>             </wrappanel>         </listbox.itemtemplate>     </listbox> 

and set binding dictionary in code after load:

mylistbox.itemssource=app.globaldictionary;  

Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -