Advertisement
Guest User

WPF SettingBinding markup extension

a guest
Oct 2nd, 2010
792
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.77 KB | None | 0 0
  1.     public class SettingsManager
  2.     {
  3.         static SettingsManager()
  4.         {
  5.             _settings = GetSettings();
  6.         }
  7.  
  8.         private static ApplicationSettingsBase GetSettings()
  9.         {
  10.             Assembly asm = AssemblyManager.GetEntryAssembly();
  11.             if (asm != null)
  12.             {
  13.                 Type settingsType = (from t in asm.GetTypes()
  14.                                      where t.IsSubclassOf(typeof(ApplicationSettingsBase))
  15.                                      select t).FirstOrDefault();
  16.                 if (settingsType != null)
  17.                 {
  18.                     PropertyInfo pi = settingsType.GetProperty("Default", BindingFlags.Public | BindingFlags.Static);
  19.                     if (pi != null)
  20.                     {
  21.                         return pi.GetValue(null, null) as ApplicationSettingsBase;
  22.                     }
  23.                     else
  24.                     {
  25.                         return Activator.CreateInstance(settingsType) as ApplicationSettingsBase;
  26.                     }
  27.                 }
  28.             }
  29.             return null;
  30.         }
  31.  
  32.         private static ApplicationSettingsBase _settings;
  33.        
  34.         public static ApplicationSettingsBase Settings
  35.         {
  36.             get { return _settings; }
  37.             set { _settings = value; }
  38.         }
  39.  
  40.     }
  41.  
  42.     public class SettingBindingExtension : Binding
  43.     {
  44.         public SettingBindingExtension()
  45.         {
  46.             Initialize();
  47.         }
  48.  
  49.         public SettingBindingExtension(string path)
  50.             : base(path)
  51.         {
  52.             Initialize();
  53.         }
  54.  
  55.         private void Initialize()
  56.         {
  57.             this.Source = SettingsManager.Settings;
  58.             this.Mode = BindingMode.TwoWay;
  59.         }
  60.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement