First, i am basically a C++ Developer. I am used to write classes to automate everything by metaprogramming. Today i got the idea to automate the synchronisation from the Settings with the UIElements.
Question 1: Does a similar project already exists?
If not: I dont search finished solutions, i need ideas and hints how to solve my task or keep me away from bad ideas.
At first i want to Synchronise:
TextBox
<->string
Checkbox
<->bool
My proposels:
Using extensions:
public static class TextBoxSynchroniseExtension { public static string load(this TextBox control, string propertyName) { string value = Properties.Settings.Default[propertyName].ToString(); control.Text = value; return value; } public static string save(this TextBox control, string propertyName) { string value = control.Text; Properties.Settings.Default[propertyName] = value; return value; } }
Disadvantages:
- I need to give the property name everytime.
- For more UIElements i have to write a new extension.
Override the UIElements:
(Untested)
public partial class SyncSettingsTextBox : TextBox { [Description("The name for synchronise with the settings."), Category("Data")] public string SettingsName{ get; set; } public SyncSettingsTextBox(string settingsName) : base() { this.SettingsName = settingsName; LoadFromSettings(); } public void LoadFromSettings() { this.Text = Properties.Settings.Default[SettingsName].ToString(); } public void saveToSettings() { Properties.Settings.Default[SettingsName] = this.Text; } }
Creating a generic
UserControl
which re-/store his child by passing the propertyname and the settings name.Pseudocode:
<StackPanel> <SynchroniseWrapper SettingsName="Name" PropertyName="Text" > <TextBox Text="I get overrided from Properties.Settings.Default.Name" /> </SynchroniseWrapper> <SynchroniseWrapper SettingsName="PWD" PropertyName="Password" > <PasswordBox /> </SynchroniseWrapper> <SynchroniseWrapper SettingsName="AutoLogin" PropertyName="IsChecked" > <CheckBox IsChecked="False" Content="Remember me?" /> </SynchroniseWrapper> </StackPanel
Question 2: Which keywords i have to lookup for creating such UserControls
.