|
4 | 4 | using System.Text.RegularExpressions;
|
5 | 5 | using System.Windows;
|
6 | 6 | using System.Windows.Media;
|
7 |
| - |
| 7 | +using System.Windows.Media.Animation; |
8 | 8 | namespace MaterialDesignThemes.Wpf
|
9 | 9 | {
|
10 | 10 | public class PaletteHelper
|
@@ -123,23 +123,35 @@ public void ReplaceAccentColor(string name)
|
123 | 123 | /// <param name="newValue">The new entry value</param>
|
124 | 124 | /// <param name="parentDictionary">The root dictionary to start searching at. Null means using Application.Current.Resources</param>
|
125 | 125 | /// <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) |
127 | 127 | {
|
| 128 | + const int DURATION_MS = 500; //Change the value if needed |
| 129 | + |
128 | 130 | if (parentDictionary == null)
|
129 | 131 | parentDictionary = Application.Current.Resources;
|
130 |
| - |
| 132 | + |
131 | 133 | if (parentDictionary.Contains(entryName))
|
132 | 134 | {
|
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 | + } |
135 | 150 | }
|
136 |
| - |
137 | 151 | foreach (var dictionary in parentDictionary.MergedDictionaries)
|
138 |
| - { |
139 | 152 | if (ReplaceEntry(entryName, newValue, dictionary))
|
140 | 153 | return true;
|
141 |
| - } |
142 |
| - |
| 154 | + |
143 | 155 | return false;
|
144 | 156 | }
|
145 | 157 | }
|
|
0 commit comments