-
Notifications
You must be signed in to change notification settings - Fork 3
/
VarsCache.cs
51 lines (45 loc) · 1.86 KB
/
VarsCache.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
public class VarsCache
{
private Dictionary<string, string> cache = new Dictionary<string, string>();
private Dictionary<TextBox, string> textbox_binding = new Dictionary<TextBox, string>();
private Dictionary<CheckBox, string> checkbox_binding = new Dictionary<CheckBox, string>();
public string GetVar(string varName, string default_value)
{
if (cache.ContainsKey(varName))
return cache[varName];
return default_value;
}
public void SetVar(string varName, string value)
{
cache[varName] = value;
}
public void BindWithTextBox(string varName, TextBox textbox)
{
textbox.Text = this.GetVar(varName, textbox.Text);
textbox.TextChanged += new TextChangedEventHandler(textbox_TextChanged);
this.textbox_binding.Add(textbox, varName);
}
void textbox_TextChanged(object sender, TextChangedEventArgs e)
{
TextBox textbox = (TextBox)e.Source;
string varName = this.textbox_binding[textbox];
SetVar(varName, textbox.Text);
}
public void BindWithCheckBox(string varName, CheckBox checkbox)
{
checkbox.IsChecked = this.GetVar(varName, checkbox.IsChecked == true ? "true" : "false") == "true";
//checkbox.Click += new RoutedEventHandler(checkbox_Click);
checkbox.Checked += new RoutedEventHandler(checkbox_Click);
checkbox.Unchecked += new RoutedEventHandler(checkbox_Click);
this.checkbox_binding.Add(checkbox, varName);
}
void checkbox_Click(object sender, RoutedEventArgs e)
{
CheckBox checkbox = (CheckBox)e.Source;
string varName = this.checkbox_binding[checkbox];
SetVar(varName, checkbox.IsChecked == true ? "true" : "false");
}
}