Open blakepell opened 3 years ago
Maybe I don't understand what the AccentColor
is meant to do. I was able to get the selected color on the Pivot tab to change but not by changing the theme managers accent color, I ended adding this (but I'm still curious what is the right way to change the accent color on the fly to something the user chooses that isn't in a full blown theme):
public static readonly DependencyProperty RandomColorProperty = DependencyProperty.Register(
nameof(RandomColor), typeof(Color), typeof(MainWindow), new PropertyMetadata(default(Color)));
public Color RandomColor
{
get => (Color) GetValue(RandomColorProperty);
set => SetValue(RandomColorProperty, value);
}
<Window.Resources>
<SolidColorBrush x:Key="TestAccentBrush" Color="{Binding RandomColor}"></SolidColorBrush>
<StaticResource x:Key="PivotHeaderItemSelectedPipeFill" ResourceKey="TestAccentBrush" />
</Window.Resources>
I ended up creating a ColorPaletteResources
that I set the Accent
on and kept a reference to. Then I added it to the Resources.MergedDictionaries
for the Window
. That seemed to do the trick in a relatively minimal amount of code. In testing I used a static property on the App.
Sharing for posterity (note: this is an proof of concept, probably not a best practice). I set the theme in the MainWindow.xaml
, then in a button event I start and endless timer that updates the accent with a random color every 100ms.
App.xaml.cs
public static ColorPaletteResources ColorPalette { get; set; }
MainWindow.xaml
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="pack://application:,,,/ModernWpf;component/ThemeResources/Dark.xaml" />
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
MainWindow.xaml.cs
private DispatcherTimer _timer;
private Random _rnd = new Random();
private void ButtonBaseRandomColor_OnClick(object sender, RoutedEventArgs e)
{
if (!this.Resources.MergedDictionaries.Contains(App.ColorPalette))
{
this.Resources.MergedDictionaries.Add(App.ColorPalette);
_timer = new DispatcherTimer();
_timer.Interval = TimeSpan.FromMilliseconds(100);
_timer.Tick += Timer_Tick;
_timer.Start();
}
int x = _rnd.Next(1, 12);
switch (x)
{
case 1:
App.ColorPalette.Accent = Colors.Green;
break;
case 2:
App.ColorPalette.Accent = Colors.DarkRed;
break;
case 3:
App.ColorPalette.Accent = Colors.Blue;
break;
case 4:
App.ColorPalette.Accent = Colors.Purple;
break;
case 5:
App.ColorPalette.Accent = Colors.DarkGoldenrod;
break;
case 6:
App.ColorPalette.Accent = Colors.Cyan;
break;
case 7:
App.ColorPalette.Accent = Colors.Fuchsia;
break;
case 8:
App.ColorPalette.Accent = Colors.Brown;
break;
case 9:
App.ColorPalette.Accent = Colors.GreenYellow;
break;
case 10:
App.ColorPalette.Accent = Colors.White;
break;
case 11:
App.ColorPalette.Accent = Colors.Gray;
break;
case 12:
App.ColorPalette.Accent = Colors.DeepPink;
break;
}
}
private void Timer_Tick(object sender, EventArgs e)
{
ButtonBaseRandomColor_OnClick(sender, null);
}
I have an issue, I'm likely missing something (I tried this through binding also with the bits from the sample app). The sample's apps color picker to set the accent is working.
Behavior: I set the
ThemeManager.Current.AccentColor
and it only sets if theme has not yet been loaded. If theLight
theme loads initially, then I set it the color never changes. If theLight
theme loads, I set theAccentColor
, then I load theDark
theme it will show for theDark
theme but then theDark
themes accent color can't be changed (it's like the color is etched in stone after used in a theme). I observe that it is changing something still (for instance, the radio button color doesn't change, but when I hover over it I can see the accent color so I know -something- is happening).I can include a repro project but it's pretty bare bones, here's my code:
App.xaml
MainWindow.xaml
MainWindow.xaml.cs
Screenshots
AccentColor set to purple, but you can see the accent colors haven't changed.
Switch to dark, you can see, it picked up the purple:
AccentColor changed to green, but as you go between light and dark they stay on whatever the first set Accent color was for that theme.