Free Code Snippets in C#, Net Framework, Office 365, ASP.Net,WPF, Visual Studio, SQL Server, Antivirus free
#

WPF: Create, write and read settings dynamically

 

Under WPF settings are stored in Settings. These are usually in development mode in an editable file.

You can also use the settings dynamically at runtime, such as adjustable user variables or app variables.

Unlike UWP, you have to create, change and read the settings yourself.

 

#UWP Settings, Settings, Properties

 

Save Settings

The settings are managed in WPF as XML file. If you manage the settings at runtime, you have to access the Settings.Default.Properties

Properties.Settings.Default.Properties

 

Within this enumeration are elements of type SettingsProperty

SettingsProperty prop

 

 

A single setting setting is queried directly with the name

//< existing Setting >

prop = Properties.Settings.Default.Properties[property_name];

//</ existing Setting >

 

If you want to create a new setting setting, you have to create a settingsproperty and add it to the Properties [].

With .Save () the setting and thus also the settings file is stored

prop = new System.Configuration.SettingsProperty(property_name);

prop.PropertyType = typeof(string);

Properties.Settings.Default.Properties.Add(prop);

Properties.Settings.Default.Save();

 

The reading and writing of values ​​in the settings I simply use the DefaultValue. There is no assignment via .value.

//< set value  >

Properties.Settings.Default.Properties[property_name].DefaultValue  = setting_Value;

//</ set value >

 

 

 

Complete setting example

In WPF I have saved the settings as defaultvalue and read. This is as a workaround since writing to a real value is not executed under Properties.

        private void save_Setting(string setting_Name, string setting_Value)

        {

            //----------< save_Settings() >----------

 

            string property_name = setting_Name;

 

            //< Setting erstellen >

            SettingsProperty prop = null;

            if (Properties.Settings.Default.Properties[property_name] != null)

            {

                //< existing Setting >

                prop = Properties.Settings.Default.Properties[property_name];

                //</ existing Setting >

            }

            else

            {

                //< new Setting >

                prop = new System.Configuration.SettingsProperty(property_name);

                prop.PropertyType = typeof(string);

                Properties.Settings.Default.Properties.Add(prop);

                Properties.Settings.Default.Save();

                //</ new Setting >

            }

            //</ Setting erstellen >

 

            //< set value  >

            Properties.Settings.Default.Properties[property_name].DefaultValue  = setting_Value;

            //</ set value >

 

            //< Save Settings File >

            Properties.Settings.Default.Save();

            //</ Save Settings File >

           

            //----------</ save_Settings() >----------

        }

 

 

 

 

 

Complete setting example

 

      #region  Region: Settings

 

        private void save_Settings()

        {           

            foreach (FrameworkElement element in CanvasMain.Children)

            {

                if (element.GetType() == typeof(UcPanel))

                {

                    UcPanel panel = element as UcPanel;                   

                    save_Setting("Panel_ID_" + panel.panel_ID, panel.panel_ID );

                    save_Setting("panel_Caption_" + panel.panel_ID, panel.panel_Caption);

                    save_Setting("panel_Background_Color_" + panel.panel_ID, panel.panel_Background_Color );

                    save_Setting("panel_Border_Thickness_" + panel.panel_ID, panel.panel_Border_Thickness.ToString()  );

                    save_Setting("panel_Left_" + panel.panel_ID, Canvas.GetLeft(panel).ToString());

                    save_Setting("panel_Top_" + panel.panel_ID, Canvas.GetTop(panel).ToString());

                    save_Setting("panel_Height_" + panel.panel_ID, panel.ActualHeight.ToString() );

                    save_Setting("panel_Width_" + panel.panel_ID, panel.ActualWidth.ToString());                  

                }

            }

        }

 

        private void read_Settings()

        {

            int posID = "Panel_ID_".Length;

            foreach (SettingsProperty setting in Properties.Settings.Default.Properties)                            {

                if (setting != null)

                {

                    if (setting.Name.Contains("Panel_ID_"))

                    {

                        UcPanel panel = new UcPanel();

 

                        string panel_ID = setting.Name.Substring(posID);

 

                        //--< setup panel >--

                        string sValue = "";

 

                        sValue = read_Setting("panel_Caption_" + panel_ID);

                        panel.panel_Caption = sValue;

 

                        sValue = read_Setting("panel_Border_Thickness_" + panel_ID);

                        panel.panel_Border_Thickness = Convert.ToInt16 ( sValue);

 

                        sValue = read_Setting("panel_Background_Color_" + panel_ID);

                        panel.panel_Background_Color  = sValue;

 

                        sValue = read_Setting("panel_Left_" + panel_ID);

                        Canvas.SetLeft(panel, Convert.ToDouble(sValue));

 

                        sValue = read_Setting("panel_Top_" + panel_ID);

                        Canvas.SetTop(panel, Convert.ToDouble(sValue));

 

                        sValue = read_Setting("panel_Height_" + panel_ID);

                        panel.Height  = Convert.ToDouble(sValue);

 

                        sValue = read_Setting("panel_Width_" + panel_ID);

                        panel.Width = Convert.ToDouble(sValue);

                        //--< setup panel >--

 

                        //< add >

                        CanvasMain.Children.Add(panel);

                        //</ add >

                    }

 

                }

            }

        }

 

        private void save_Setting(string setting_Name, string setting_Value)

        {

            //----------< save_Settings() >----------

 

            string property_name = setting_Name;

 

            //< Setting erstellen >

            SettingsProperty prop = null;

            if (Properties.Settings.Default.Properties[property_name] != null)

            {

                //< existing Setting >

                prop = Properties.Settings.Default.Properties[property_name];

                //</ existing Setting >

            }

            else

            {

                //< new Setting >

                prop = new System.Configuration.SettingsProperty(property_name);

                prop.PropertyType = typeof(string);

                Properties.Settings.Default.Properties.Add(prop);

                Properties.Settings.Default.Save();

                //</ new Setting >

            }

            //</ Setting erstellen >

 

            //< set value  >

            Properties.Settings.Default.Properties[property_name].DefaultValue  = setting_Value;

            //</ set value >

 

            //< Save Settings File >

            Properties.Settings.Default.Save();

            //</ Save Settings File >

           

            //----------</ save_Settings() >----------

        }

 

        private string read_Setting(string setting_Name)

        {

            //----------< save_Settings() >----------

            string sResult = "";

 

            //< get Setting >

            if (Properties.Settings.Default.Properties[setting_Name] != null)

            {

                sResult = Properties.Settings.Default.Properties[setting_Name].DefaultValue.ToString() ;               

            }

            //</ get Setting >

 

            //< correct >

            if (sResult == "NaN") sResult = "0";

            //</ correct >

 

            //< output >

            return sResult;

            //</ output >

 

            //----------</ save_Settings() >----------

        }

        #endregion  /Region: Settings

 

 

Screenshot:

Settings settings

 

Settings View Code

Mobile
»
WPF: Create, write and read settings dynamically
»
WPF: Create settings at runtime

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de