Click here to Skip to main content
15,886,067 members
Articles / Programming Languages / C#

Persisting Modern UI for WPF Styles

Rate me:
Please Sign up or sign in to vote.
4.86/5 (6 votes)
14 May 2013CPOL1 min read 24.3K   8   3
Persisting modern UI for WPF Styles

Introduction

About 2 weeks ago, I blogged about Creating a Modern UI for WPF, the template by default doesn’t persist styles selected in the settings menu through application launches. So I’m going to give you the steps required to make this feature happen to complete feel of the template Smile.

First, follow all the steps in the previous post, then you will need to add a new settings file to the project.

image

Next, you will need to add 4 settings:

  1. Name: SelectedAccentColor, Type: System.Windows.Media.Color, Scope: User, Value: #FF1BA1E2
  2. Name: SelectedThemeSource, Type: System.Uri, Scope: User, Value: /FirstFloor.ModernUI;component/Assets/ModernUI.Dark.xaml
  3. Name: SelectedThemeDisplayName, Type: string, Scope: User, Value: dark
  4. Name: SelectedFontSize, Type: string, Scope: User, Value: large

Next, you will browse to Content\SettingsAppearanceViewModel.cs, add a private member bool called _colorLoadedYet with a default value of false.

C#
private bool _colorLoadedYet;

Now look for a method called SyncThemeAndColor and paste the code below at the bottom of that method:

C#
if (this._colorLoadedYet)
{
    ApplicationSettings.Default.SelectedThemeDisplayName = this.SelectedTheme.DisplayName;
    ApplicationSettings.Default.SelectedThemeSource = this.SelectedTheme.Source;
    ApplicationSettings.Default.SelectedAccentColor = this.SelectedAccentColor;
    ApplicationSettings.Default.SelectedFontSize = this.SelectedFontSize;
    ApplicationSettings.Default.Save();
}

Also, add a method called SetThemeAndColor as below:

C#
public void SetThemeAndColor
(string themeSourceDisplayName, Uri themeSourceUri, Color accentColor, string fontSize)
{
    this.SelectedTheme = new Link 
    { DisplayName = themeSourceDisplayName, Source = themeSourceUri };
    this.SelectedAccentColor = accentColor;
    this.SelectedFontSize = fontSize;
    this._colorLoadedYet = true;
}

For the font size setting, we are going to go to the SelectedFontSize property and code below after the line this.OnPropertyChanged(“SelectedFontSize”);

C#
if (_colorLoadedYet)
{
    ApplicationSettings.Default.SelectedFontSize = this.selectedFontSize;
    ApplicationSettings.Default.Save();
}

And for the final step, you must browse to the MainWindow.xaml.cs file and just after the InitializeComponent, add the code below:

C#
SettingsAppearanceViewModel settings = new SettingsAppearanceViewModel();
settings.SetThemeAndColor(ApplicationSettings.Default.SelectedThemeDisplayName,
      ApplicationSettings.Default.SelectedThemeSource, 
      ApplicationSettings.Default.SelectedAccentColor, 
      ApplicationSettings.Default.SelectedFontSize);

That’s all, you can now run your application and change the settings and they will persist through application launches. Through development, this will reset often but won’t while your application is used by a user. For the best results, replace the settings file with a more permanent storage location.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SSW
South Africa South Africa

Comments and Discussions

 
QuestionFontSize not persisting Pin
Mohammed Abdul Mateen25-May-14 20:48
Mohammed Abdul Mateen25-May-14 20:48 
AnswerRe: FontSize not persisting Pin
Mohammed Abdul Mateen25-May-14 21:13
Mohammed Abdul Mateen25-May-14 21:13 
QuestionCool tip. Pin
Valery Possoz23-Jan-14 0:51
professionalValery Possoz23-Jan-14 0:51 
Thanks for sharing this, ModernUI is a great and simple framework.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.