C
C#β€’13mo ago
ricecracker2234

❔ How do i handle user settings in wpf .net core

How do i handle user settings in WPF .NET 6?
14 Replies
Pobiega
Pobiegaβ€’13mo ago
Define "user settings"? M.E.C doesnt support two-way config updates very well, so it might not be ideal for that usecase.
ricecracker2234
ricecracker2234β€’13mo ago
basically settings that the end-users can change
Pobiega
Pobiegaβ€’13mo ago
by editing the config file manually, or through some WPF interface in your app?
ricecracker2234
ricecracker2234β€’13mo ago
through the wpf interface with bindings
Pobiega
Pobiegaβ€’13mo ago
Right. Thats unfortunately not the best usecase for M.E.C afaik
ricecracker2234
ricecracker2234β€’13mo ago
oh
Pobiega
Pobiegaβ€’13mo ago
it has good support for reloading the file as it changes, but it doesnt have two-way binding support
ricecracker2234
ricecracker2234β€’13mo ago
so what's a good library for handling user settings in general?
Pobiega
Pobiegaβ€’13mo ago
Whats wrong with System.Configuration? it supports two-way binding
ricecracker2234
ricecracker2234β€’13mo ago
does it support other types like enums?
Pobiega
Pobiegaβ€’13mo ago
I have no idea. Lets try. can confirm, works fine with enums strings, ints are all fine too it gets xml serialized by default
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="TestSettings" type="WpfApp1.TestSettings, WpfApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<TestSettings OptIn="HellYes" />
</configuration>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="TestSettings" type="WpfApp1.TestSettings, WpfApp1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<TestSettings OptIn="HellYes" />
</configuration>
public class TestSettings : ConfigurationSection
{
public const string SectionName = nameof(TestSettings);

[ConfigurationProperty(nameof(OptIn), DefaultValue = CoolEnum.Unknown)]
public CoolEnum OptIn
{
get => (CoolEnum)this[nameof(OptIn)];
set => this[nameof(OptIn)] = value;
}
}

public enum CoolEnum
{
Unknown,
HellYes,
PleaseNo
}
public class TestSettings : ConfigurationSection
{
public const string SectionName = nameof(TestSettings);

[ConfigurationProperty(nameof(OptIn), DefaultValue = CoolEnum.Unknown)]
public CoolEnum OptIn
{
get => (CoolEnum)this[nameof(OptIn)];
set => this[nameof(OptIn)] = value;
}
}

public enum CoolEnum
{
Unknown,
HellYes,
PleaseNo
}
I made a shitty window that has a TestSettings instance (added and fetched from the Configuration object made by ConfigurationManager.OpenExeConfiguration) set as its datacontext and bound it there. Works fine setting values, changing values, restarting the program etc.
Unknown User
Unknown Userβ€’13mo ago
Message Not Public
Sign In & Join Server To View
Pobiega
Pobiegaβ€’13mo ago
Thats fair. Set up a simple POCO object for your config, add some Load/Save methods and you're done thats probably a lot easier πŸ˜„
Accord
Accordβ€’13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.