0

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:

  1. 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.
  2. 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;
        }
    }
    
  3. 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.

Syrlia
  • 154
  • 2
  • 14
  • The marked answer seems not to help me, because the `ItemSource` Property exists only for [IEnumerable](https://msdn.microsoft.com/en-us/library/system.windows.controls.itemscontrol.itemssource%28v=vs.110%29.aspx). An other [answer](https://stackoverflow.com/a/846297/5064526) works, but I need to understand how it works. I dont want to save the settings instantly, only if some criterias are succuessfull. – Syrlia Aug 24 '17 at 14:25
  • why would you bind ItemsSource? Bind TextBox.Text and CheckBox.IsChecked to a string and a bool properties – ASh Aug 24 '17 at 14:26
  • Okay, I played something with it and figured out how it works. Thanks! – Syrlia Aug 24 '17 at 14:34

0 Answers0