Skip to content

Commit

Permalink
Added OK Button in ColorPicker demo tool window to change AccentColor…
Browse files Browse the repository at this point in the history
… at run time.
  • Loading branch information
Dirkster99 committed Feb 14, 2019
1 parent 69efa5e commit 0ba57c1
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
58 changes: 58 additions & 0 deletions source/MLibTest/MLibTest/Demos/ViewModels/ColorPickerViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
using System;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Input;
using MLibTest.ViewModels.Base;
using MLib.Interfaces;
using System.Windows;
using Settings.Interfaces;

/// <summary>
/// Implements the viewmodel that drives the view a Color Picker tool window.
Expand All @@ -27,6 +32,7 @@ internal class ColorPickerViewModel : ToolViewModel

private Color _SelectedBackgroundColor;
private Color _SelectedAccentColor;
private ICommand _ResetAccentColorCommand;
#endregion fields

#region constructors
Expand Down Expand Up @@ -88,6 +94,58 @@ public Color SelectedAccentColor
}
}

/// <summary>
/// Gets a command to reset the currently selected accent color
/// and reloads all current resources to make sure that the
/// accent is changed consistently.
/// </summary>
public ICommand ResetAccentColorCommand
{
get
{
if (_ResetAccentColorCommand == null)
{
_ResetAccentColorCommand = new RelayCommand<object>((p) =>
{
if ((p is Color) == false)
return;

Color accentColor = (Color)p;

var appearance = GetService<IAppearanceManager>();
var settings = GetService<ISettingsManager>(); // add the default themes

// 1) You could use this if you where using MLib only
// appearance.SetAccentColor(accentColor);

// 2) But you should use this if you use MLib with additional libraries
// with additional accent colors to be synchronized at run-time
appearance.SetTheme(settings.Themes
, appearance.ThemeName
, accentColor);

// 3 You could also use something like this to change accent color
// If you were using your own Theming Framework or MUI, Mahapps etc
//
//// Application.Current.Resources[MWindowLib.Themes.ResourceKeys.ControlAccentColorKey] = accentColor;
//// Application.Current.Resources[MWindowLib.Themes.ResourceKeys.ControlAccentBrushKey] = new SolidColorBrush(accentColor);
////
//// Application.Current.Resources[MLib.Themes.ResourceKeys.ControlAccentColorKey] = accentColor;
//// Application.Current.Resources[MLib.Themes.ResourceKeys.ControlAccentBrushKey] = new SolidColorBrush(accentColor);
////
//// Application.Current.Resources[Xceed.Wpf.AvalonDock.Themes.VS2013.Themes.ResourceKeys.ControlAccentColorKey] = accentColor;
//// Application.Current.Resources[Xceed.Wpf.AvalonDock.Themes.VS2013.Themes.ResourceKeys.ControlAccentBrushKey] = new SolidColorBrush(accentColor);
////
//// Application.Current.Resources[NumericUpDownLib.Themes.ResourceKeys.ControlAccentColorKey] = accentColor;
//// Application.Current.Resources[NumericUpDownLib.Themes.ResourceKeys.ControlAccentBrushKey] = new SolidColorBrush(accentColor);

});
}

return _ResetAccentColorCommand;
}
}

/// <summary>
/// Gets a human readable description for the <see ref="SelectedAccentColor"/> property.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
HorizontalAlignment="Stretch"
Margin="0"
/>

<Button Content="OK"
Command="{Binding ResetAccentColorCommand}"
CommandParameter="{Binding ElementName=_colorCanvas, Path=SelectedColor}"
HorizontalAlignment="Left"
/>
</StackPanel>
</Grid>
</UserControl>
4 changes: 2 additions & 2 deletions source/MLibTest/MLibTest/MLibTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
<Compile Include="Demos\ViewModels\ColorPickerViewModel.cs" />
<Compile Include="Demos\ViewModels\Interfaces\IWorkSpaceViewModel.cs" />
<Compile Include="Demos\ViewModels\WorkSpaceViewModel.cs" />
<Compile Include="Demos\Views\ColorSelectionView.xaml.cs">
<Compile Include="Demos\ViewModels\Views\ColorSelectionView.xaml.cs">
<DependentUpon>ColorSelectionView.xaml</DependentUpon>
</Compile>
<Compile Include="Models\AppCore.cs" />
Expand Down Expand Up @@ -116,7 +116,7 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="Demos\Views\ColorSelectionView.xaml">
<Page Include="Demos\ViewModels\Views\ColorSelectionView.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
Expand Down

0 comments on commit 0ba57c1

Please sign in to comment.