Skip to content
Ollie Riches edited this page Jan 7, 2014 · 3 revisions

The following guide will walk you through getting started with Simple.Wpf.Themes.

Installation

The simplest & quickest way to install is via NuGet. Simply issue the following command at the Package Manager Console in Visual Studio:

PM> Install-Package Simple.Wpf.Themes

Namespaces

There is only a single namespaces you'll need, this contains the Theme user control & the ThemeManager class.

using Simple.Wpf.Themes;

Examples

The control supports being used in both code-behind & MVVM approaches, personally I prefer an MVVM approach but I've made sure it works from a code-behind point of view as well.

Code behind implementation

This example creates an array of Theme objects in the constructor of the code behind for the main window and this is then set to the ItemsSource of the Theme user control. The user control uses a static ThemeManager class internally to manipulate the current theme.

public MainWindow()
{
    InitializeComponent();

    var themes = new[]
    {
        new Theme("No theme (default)", null),
        new Theme("Red theme", new Uri("/Wpf.TestHarness;component/Themes/RedTheme.xaml", UriKind.Relative)),
        new Theme("Blue theme", new Uri("/Wpf.TestHarness;component/Themes/BlueTheme.xaml", UriKind.Relative))
    };

    ThemesControl.ItemsSource = themes;
}

XAML:

<Grid>
        
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
        
    <themes:Themes x:Name="ThemesControl"
                    Grid.Row="0"
                    Margin="5"/>
        
</Grid>

MVVM implementation

This example creates the array of Theme objects in the ViewModel, the Themes are bound in the XAML:

public sealed class MainViewModel : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    private Theme _selectedTheme;

    public MainViewModel()
    {
        Themes = new[]
                    {
                        new Theme("No theme (default)", null),
                        new Theme("Red theme", new Uri("/Wpf.Mvvm.TestHarness;component/Themes/RedTheme.xaml", UriKind.Relative)),
                        new Theme("Blue theme", new Uri("/Wpf.Mvvm.TestHarness;component/Themes/BlueTheme.xaml", UriKind.Relative))
                    };

        _selectedTheme = Themes.First();
    }

    public IEnumerable<Theme> Themes { get; private set; }

    public Theme SelectedTheme
    {
        get
        {
            return _selectedTheme;
        }
        set
        {
            _selectedTheme = value;
            OnPropertyChanged("SelectedTheme");
        }
    }

    private void OnPropertyChanged(string propertyName)
    {
        var handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

XAML:

<Grid>

    <Grid.RowDefinitions>
        <RowDefinition Height="Auto" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>

    <themes:Themes x:Name="ThemesControl"
                    Grid.Row="0"
                    Margin="5"
                    ItemsSource="{Binding Path=Themes,Mode=OneWay}" 
                    SelectedItem="{Binding Path=SelectedTheme, Mode=TwoWay}"/>

</Grid>
Clone this wiki locally