daily pastebin goal
4%
SHARE
TWEET

Untitled

a guest Feb 13th, 2018 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <ComboBox ItemsSource="{Binding Settings}" DisplayMemberPath="Name"
  2.           SelectedItem="{Binding SelectedSetting}" />
  3.    
  4. public ObservableCollection<UserSettings> Settings { get; set; }
  5.  
  6. private UserSettings _selectedSetting;
  7. public UserSettings SelectedSetting
  8. {
  9.     get => _selectedSetting;
  10.     set
  11.     {
  12.         _selectedSetting = value;
  13.         OnPropertyChanged("SelectedSetting");
  14.         OnPropertyChanged("UserStringSettingsHandler");
  15.         OnPropertyChanged("UserSlideSettingsHandler");
  16.     }
  17. }
  18.  
  19. public UserSettingsViewModelHandler<string> UserStringSettingsHandler { get; set; }
  20. public UserSettingsViewModelHandler<SlideSettings> UserSlideSettingsHandler { get; set; }
  21.    
  22. public class ApplicationSettingsViewModelHandler<TReturnType> : AbstractApplicationSettingsViewModelHandler<AppSettings, TReturnType>, INotifyPropertyChanged
  23. {
  24.     public ApplicationSettingsViewModelHandler(AppSettings settings) : base(settings)
  25.     {
  26.     }
  27. }
  28.  
  29. public class UserSettingsViewModelHandler<TReturnType> : AbstractApplicationSettingsViewModelHandler<UserSettings, TReturnType>, INotifyPropertyChanged
  30. {
  31.     public UserSettingsViewModelHandler(UserSettings settings) : base(settings)
  32.     {
  33.     }
  34. }
  35.  
  36. public abstract class AbstractApplicationSettingsViewModelHandler<TSettingsType, TReturnType> : INotifyPropertyChanged
  37. {
  38.     private TSettingsType _settings;
  39.  
  40.     public event PropertyChangedEventHandler PropertyChanged;
  41.  
  42.     public AbstractApplicationSettingsViewModelHandler(TSettingsType settings)
  43.     {
  44.         _settings = settings;
  45.         _values = new Dictionary<string, TReturnType>();
  46.  
  47.         MapSettings();
  48.  
  49.         PropertyChanged += OnApplicationSettingsChanged;
  50.     }
  51.  
  52.     private void MapSettings()
  53.     {
  54.         var settingsType = typeof(TSettingsType);
  55.  
  56.         var properties = settingsType.GetProperties().Where(prop => prop.CanRead && prop.CanWrite);
  57.         foreach(var prop in properties)
  58.         {
  59.             try
  60.             {
  61.                 if (prop.GetValue(_settings) is TReturnType val)
  62.                 {
  63.                     this[prop.Name] = val;
  64.                 }
  65.             } catch(TargetParameterCountException) { }
  66.         }
  67.     }
  68.  
  69.     private Dictionary<string, TReturnType> _values;
  70.     public TReturnType this[string key]
  71.     {
  72.         get => _values[key];
  73.         set
  74.         {
  75.             _values[key] = value;
  76.             OnPropertyChanged(key);
  77.         }
  78.     }
  79.  
  80.     private void OnApplicationSettingsChanged(object sender, PropertyChangedEventArgs args)
  81.     {
  82.         if (!_values.ContainsKey(args.PropertyName))
  83.             return;
  84.  
  85.         var settingsType = typeof(TSettingsType);
  86.  
  87.         var property = settingsType
  88.             .GetProperties()
  89.             .Where(prop => prop.CanRead && prop.CanWrite)
  90.             .FirstOrDefault(prop => prop.Name == args.PropertyName);
  91.  
  92.         if(property != null)
  93.         {
  94.             property.SetValue(_settings, this[args.PropertyName], null);
  95.             AppSettings.Default.Save();
  96.         }
  97.     }
  98.  
  99.     public void OnPropertyChanged(string propertyName)
  100.     {
  101.         PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  102.     }
  103. }
  104.    
  105. public ReportingWindowViewModel()
  106. {
  107.     Settings = new ObservableCollection<UserSettings>(AppSettings.Default.AllUserSettings);
  108.  
  109.  
  110.     AnalyticsHandler = new AnalyticsViewModelHandler<ReportingWindowViewModel>(this);
  111.  
  112.     PropertyChanged += OnSelectedSetting;
  113.  
  114.     SelectedSetting = AppSettings.Default.CurrentUserSettings;
  115. }
  116.  
  117. private void OnSelectedSetting(object sender, PropertyChangedEventArgs args)
  118. {
  119.     if (args.PropertyName != "SelectedSetting")
  120.         return;
  121.  
  122.     var appSettings = AppSettings.Default;
  123.  
  124.     var selectedSettingsIndex = Settings.IndexOf(SelectedSetting);
  125.     appSettings.SetCurrentUserSettings(selectedSettingsIndex);
  126.  
  127.     UserStringSettingsHandler = new UserSettingsViewModelHandler<string>(appSettings.CurrentUserSettings);
  128.     UserSlideSettingsHandler = new UserSettingsViewModelHandler<SlideSettings>(appSettings.CurrentUserSettings);
  129. }
  130.    
  131. <TextBox Text="{Binding UserStringSettingsHandler[Name]}" />
RAW Paste Data
Top