Skip to content

Commit 478b2be

Browse files
committed
Added fade animation to palette helper
1 parent 403d227 commit 478b2be

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

MaterialDesignThemes.Wpf/PaletteHelper.cs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using System.Text.RegularExpressions;
55
using System.Windows;
66
using System.Windows.Media;
7-
7+
using System.Windows.Media.Animation;
88
namespace MaterialDesignThemes.Wpf
99
{
1010
public class PaletteHelper
@@ -123,23 +123,35 @@ public void ReplaceAccentColor(string name)
123123
/// <param name="newValue">The new entry value</param>
124124
/// <param name="parentDictionary">The root dictionary to start searching at. Null means using Application.Current.Resources</param>
125125
/// <returns>Weather the value was replaced (true) or not (false)</returns>
126-
private static bool ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null)
126+
private static bool ReplaceEntry(object entryName, object newValue, ResourceDictionary parentDictionary = null, bool animate)
127127
{
128+
const int DURATION_MS = 500; //Change the value if needed
129+
128130
if (parentDictionary == null)
129131
parentDictionary = Application.Current.Resources;
130-
132+
131133
if (parentDictionary.Contains(entryName))
132134
{
133-
parentDictionary[entryName] = newValue;
134-
return true;
135+
if (animate) //Fade animation is enabled
136+
{
137+
ColorAnimation animation = new ColorAnimation()
138+
{
139+
From = (Color)parentDictionary[entryName],//The old color
140+
To = (Color)newValue, //The new color
141+
Duration = new Duration(new TimeSpan(0,0,0,0,DURATION_MS))
142+
};
143+
(parentDictionary[entryName] as Brush).BeginAnimation(SolidColorBrush.ColorProperty, animation); //Begin the animation
144+
return true;
145+
}
146+
else
147+
{
148+
parentDictionary[entryName] = newvalue;
149+
}
135150
}
136-
137151
foreach (var dictionary in parentDictionary.MergedDictionaries)
138-
{
139152
if (ReplaceEntry(entryName, newValue, dictionary))
140153
return true;
141-
}
142-
154+
143155
return false;
144156
}
145157
}

0 commit comments

Comments
 (0)