Skip to content

Commit 3b6da31

Browse files
committed
improve hover effect, fix text field opacity
1 parent d162e02 commit 3b6da31

File tree

5 files changed

+111
-39
lines changed

5 files changed

+111
-39
lines changed

MainDemo.Wpf/TextFields.xaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@
9494
wpf:TextFieldAssist.Hint="Phone"
9595
/>
9696
<TextBlock Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" Margin="16 0 8 0">Fruit</TextBlock>
97-
<ComboBox Grid.Row="1" Grid.Column="3" wpf:TextFieldAssist.Hint="Search" IsEditable="True" wpf:TextFieldAssist.HintOpacity=".5">
97+
<ComboBox Grid.Row="1" Grid.Column="3" wpf:TextFieldAssist.Hint="Search" IsEditable="True" wpf:TextFieldAssist.HintOpacity=".26">
9898
<ComboBoxItem>Apple</ComboBoxItem>
9999
<ComboBoxItem>Banana</ComboBoxItem>
100100
<ComboBoxItem>Pear</ComboBoxItem>

MaterialDesignThemes.Wpf/Converters/ShadowConverter.cs

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,38 +6,52 @@
66
using System.Threading.Tasks;
77
using System.Windows;
88
using System.Windows.Data;
9+
using System.Windows.Media.Effects;
910

1011
namespace MaterialDesignThemes.Wpf.Converters
1112
{
1213
public class ShadowConverter : IValueConverter
1314
{
14-
private static readonly IDictionary<ShadowDepth, object> ShadowsDictionary;
15+
private static readonly IDictionary<ShadowDepth, DropShadowEffect> ShadowsDictionary;
1516
public static readonly ShadowConverter Instance = new ShadowConverter();
1617

1718
static ShadowConverter()
1819
{
19-
var resourceDictionary = new ResourceDictionary { Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Shadows.xaml", UriKind.Absolute) };
20+
var resourceDictionary = new ResourceDictionary { Source = new Uri("pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Shadows.xaml", UriKind.Absolute) };
2021

21-
ShadowsDictionary = new Dictionary<ShadowDepth, object>
22+
ShadowsDictionary = new Dictionary<ShadowDepth, DropShadowEffect>
2223
{
23-
{ ShadowDepth.Depth1, resourceDictionary["MaterialDesignShadowDepth1"] },
24-
{ ShadowDepth.Depth2, resourceDictionary["MaterialDesignShadowDepth2"] },
25-
{ ShadowDepth.Depth3, resourceDictionary["MaterialDesignShadowDepth3"] },
26-
{ ShadowDepth.Depth4, resourceDictionary["MaterialDesignShadowDepth4"] },
27-
{ ShadowDepth.Depth5, resourceDictionary["MaterialDesignShadowDepth5"] },
24+
{ ShadowDepth.Depth1, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth1"] },
25+
{ ShadowDepth.Depth2, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth2"] },
26+
{ ShadowDepth.Depth3, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth3"] },
27+
{ ShadowDepth.Depth4, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth4"] },
28+
{ ShadowDepth.Depth5, (DropShadowEffect)resourceDictionary["MaterialDesignShadowDepth5"] },
2829
};
2930
}
3031

3132
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
3233
{
3334
if (!(value is ShadowDepth)) return null;
3435

35-
return ShadowsDictionary[(ShadowDepth) value];
36+
return Clone(ShadowsDictionary[(ShadowDepth) value]);
3637
}
3738

3839
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
3940
{
4041
throw new NotImplementedException();
4142
}
43+
44+
private static DropShadowEffect Clone(DropShadowEffect dropShadowEffect)
45+
{
46+
return new DropShadowEffect()
47+
{
48+
BlurRadius = dropShadowEffect.BlurRadius,
49+
Color = dropShadowEffect.Color,
50+
Direction = dropShadowEffect.Direction,
51+
Opacity = dropShadowEffect.Opacity,
52+
RenderingBias = dropShadowEffect.RenderingBias,
53+
ShadowDepth = dropShadowEffect.ShadowDepth
54+
};
55+
}
4256
}
4357
}

MaterialDesignThemes.Wpf/ShadowAssist.cs

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using System;
12
using System.Windows;
3+
using System.Windows.Media.Animation;
4+
using System.Windows.Media.Effects;
5+
using System.Windows.Navigation;
26

37
namespace MaterialDesignThemes.Wpf
48
{
@@ -11,6 +15,16 @@ public enum ShadowDepth
1115
Depth5
1216
}
1317

18+
internal class ShadowLocalInfo
19+
{
20+
public ShadowLocalInfo(double standardOpacity)
21+
{
22+
StandardOpacity = standardOpacity;
23+
}
24+
25+
public double StandardOpacity { get; }
26+
}
27+
1428
public static class ShadowAssist
1529
{
1630
public static readonly DependencyProperty ShadowDepthProperty = DependencyProperty.RegisterAttached(
@@ -25,6 +39,61 @@ public static ShadowDepth GetShadowDepth(DependencyObject element)
2539
{
2640
return (ShadowDepth) element.GetValue(ShadowDepthProperty);
2741
}
28-
29-
}
42+
43+
private static readonly DependencyPropertyKey LocalInfoPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
44+
"LocalInfo", typeof (ShadowLocalInfo), typeof (ShadowAssist), new PropertyMetadata(default(ShadowLocalInfo)));
45+
46+
private static void SetLocalInfo(DependencyObject element, ShadowLocalInfo value)
47+
{
48+
element.SetValue(LocalInfoPropertyKey, value);
49+
}
50+
51+
private static ShadowLocalInfo GetLocalInfo(DependencyObject element)
52+
{
53+
return (ShadowLocalInfo) element.GetValue(LocalInfoPropertyKey.DependencyProperty);
54+
}
55+
56+
public static readonly DependencyProperty DarkenProperty = DependencyProperty.RegisterAttached(
57+
"Darken", typeof (bool), typeof (ShadowAssist), new PropertyMetadata(default(bool), DarkenPropertyChangedCallback));
58+
59+
private static void DarkenPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
60+
{
61+
var uiElement = dependencyObject as UIElement;
62+
var dropShadowEffect = uiElement?.Effect as DropShadowEffect;
63+
64+
if (dropShadowEffect == null) return;
65+
66+
if ((bool) dependencyPropertyChangedEventArgs.NewValue)
67+
{
68+
SetLocalInfo(dependencyObject, new ShadowLocalInfo(dropShadowEffect.Opacity));
69+
70+
var doubleAnimation = new DoubleAnimation(1, new Duration(TimeSpan.FromMilliseconds(350)))
71+
{
72+
FillBehavior = FillBehavior.HoldEnd
73+
};
74+
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);
75+
}
76+
else
77+
{
78+
var shadowLocalInfo = GetLocalInfo(dependencyObject);
79+
if (shadowLocalInfo == null) return;
80+
81+
var doubleAnimation = new DoubleAnimation(shadowLocalInfo.StandardOpacity, new Duration(TimeSpan.FromMilliseconds(350)))
82+
{
83+
FillBehavior = FillBehavior.HoldEnd
84+
};
85+
dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty, doubleAnimation);
86+
}
87+
}
88+
89+
public static void SetDarken(DependencyObject element, bool value)
90+
{
91+
element.SetValue(DarkenProperty, value);
92+
}
93+
94+
public static bool GetDarken(DependencyObject element)
95+
{
96+
return (bool) element.GetValue(DarkenProperty);
97+
}
98+
}
3099
}

MaterialDesignThemes.Wpf/TextFieldAssist.cs

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public static class TextFieldAssist
3838
"HintOpacity",
3939
typeof(double),
4040
typeof(TextFieldAssist),
41-
new PropertyMetadata(.48, HintOpacityPropertyChangedCallback));
41+
new PropertyMetadata(.48));
4242

4343
/// <summary>
4444
/// Internal framework use only.
@@ -49,7 +49,7 @@ public static class TextFieldAssist
4949
private static readonly DependencyPropertyKey IsNullOrEmptyPropertyKey = DependencyProperty.RegisterAttachedReadOnly(
5050
"IsNullOrEmpty", typeof(bool), typeof(TextFieldAssist), new PropertyMetadata(true));
5151

52-
public static readonly DependencyProperty IsNullOrEmptyProperty =
52+
private static readonly DependencyProperty IsNullOrEmptyProperty =
5353
IsNullOrEmptyPropertyKey.DependencyProperty;
5454

5555
#endregion
@@ -172,34 +172,16 @@ private static void TextBoxViewMarginPropertyChangedCallback(
172172

173173
if (box.IsLoaded)
174174
{
175-
ApplyTextBoxViewMargin(box, (Thickness)dependencyPropertyChangedEventArgs.NewValue);
175+
ApplyTextBoxViewMargin(box, (Thickness) dependencyPropertyChangedEventArgs.NewValue);
176176
}
177177

178178
box.Loaded += (sender, args) =>
179179
{
180-
var textBox = (Control)sender;
180+
var textBox = (Control) sender;
181181
ApplyTextBoxViewMargin(textBox, GetTextBoxViewMargin(textBox));
182182
};
183183
}
184184

185-
/// <summary>
186-
/// Hints the opacity property changed callback.
187-
/// </summary>
188-
/// <param name="dependencyObject">The dependency object.</param>
189-
/// <param name="dependencyPropertyChangedEventArgs">The <see cref="DependencyPropertyChangedEventArgs" /> instance containing the event data.</param>
190-
private static void HintOpacityPropertyChangedCallback(
191-
DependencyObject dependencyObject,
192-
DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
193-
{
194-
var control = dependencyObject as Control; //could be a text box or password box
195-
if (control == null)
196-
{
197-
return;
198-
}
199-
200-
control.Opacity = (double)dependencyPropertyChangedEventArgs.NewValue;
201-
}
202-
203185
private static void TextPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
204186
{
205187
SetIsNullOrEmpty(dependencyObject, string.IsNullOrEmpty((dependencyPropertyChangedEventArgs.NewValue ?? "").ToString()));

MaterialDesignThemes.Wpf/Themes/MaterialDesignTheme.Button.xaml

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
<Setter Property="Background" Value="{DynamicResource PrimaryHueMidBrush}"/>
2626
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}"/>
2727
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
28+
<Setter Property="Cursor" Value="Hand"/>
2829
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth1" />
2930
<Setter Property="TextBlock.FontWeight" Value="DemiBold"/>
3031
<Setter Property="TextBlock.FontSize" Value="15"/>
@@ -61,6 +62,7 @@
6162
</ControlTemplate.Resources>
6263
<Grid>
6364
<Border Background="{TemplateBinding Background}"
65+
x:Name="ShadowBorder"
6466
Effect="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(wpf:ShadowAssist.ShadowDepth), Converter={x:Static converters:ShadowConverter.Instance}}" />
6567
<Border Background="{TemplateBinding Background}" x:Name="border" CornerRadius="2" />
6668
<Border Background="White" CornerRadius="2" x:Name="ClickBorder" Opacity="0" RenderTransformOrigin="0.5,0.5" >
@@ -85,7 +87,8 @@
8587
<Setter Property="BorderBrush" TargetName="border" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
8688
</Trigger>
8789
<Trigger Property="IsMouseOver" Value="true">
88-
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource AttentionToActionBrush}" />
90+
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource AttentionToActionBrush}" />
91+
<Setter TargetName="ShadowBorder" Property="wpf:ShadowAssist.Darken" Value="True" />
8992
</Trigger>
9093
<Trigger Property="IsEnabled" Value="false">
9194
<Setter Property="Opacity" Value="0.23"/>
@@ -121,6 +124,7 @@
121124
<Setter Property="FocusVisualStyle" Value="{StaticResource FocusVisual}"/>
122125
<Setter Property="Background" Value="Transparent"/>
123126
<Setter Property="BorderBrush" Value="Transparent"/>
127+
<Setter Property="Cursor" Value="Hand"/>
124128
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidBrush}"/>
125129
<Setter Property="TextBlock.FontWeight" Value="DemiBold"/>
126130
<Setter Property="BorderThickness" Value="1"/>
@@ -180,6 +184,7 @@
180184
</Trigger>
181185
<Trigger Property="IsMouseOver" Value="true">
182186
<Setter Property="BorderBrush" TargetName="border" Value="{StaticResource AttentionToActionBrush}" />
187+
<Setter Property="Background" TargetName="border" Value="{DynamicResource MaterialDesignFlatButtonClick}" />
183188
</Trigger>
184189
<Trigger Property="IsEnabled" Value="false">
185190
<Setter Property="Opacity" Value="0.23"/>
@@ -196,7 +201,7 @@
196201
<Style x:Key="MaterialDesignToolButton" TargetType="{x:Type Button}" BasedOn="{StaticResource MaterialDesignFlatButton}">
197202
<Setter Property="Foreground" Value="#616161"/>
198203
<Setter Property="Padding" Value="4"/>
199-
<Setter Property="wpf:RippleAssist.ClipToBounds" Value="False"/>
204+
<Setter Property="wpf:RippleAssist.ClipToBounds" Value="False"/>
200205
<Setter Property="Template">
201206
<Setter.Value>
202207
<ControlTemplate TargetType="Button">
@@ -206,8 +211,6 @@
206211
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
207212
Padding="{TemplateBinding Padding}"
208213
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
209-
<ControlTemplate.Triggers>
210-
</ControlTemplate.Triggers>
211214
</ControlTemplate>
212215
</Setter.Value>
213216
</Setter>
@@ -223,9 +226,10 @@
223226
<Setter Property="BorderBrush" Value="{DynamicResource PrimaryHueMidBrush}"/>
224227
<Setter Property="Foreground" Value="{DynamicResource PrimaryHueMidForegroundBrush}"/>
225228
<Setter Property="BorderThickness" Value="1"/>
229+
<Setter Property="Cursor" Value="Hand"/>
226230
<Setter Property="HorizontalContentAlignment" Value="Center"/>
227231
<Setter Property="VerticalContentAlignment" Value="Center"/>
228-
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth3" />
232+
<Setter Property="wpf:ShadowAssist.ShadowDepth" Value="Depth2" />
229233
<Setter Property="Padding" Value="1"/>
230234
<Setter Property="Width" Value="40" />
231235
<Setter Property="Height" Value="40" />
@@ -252,6 +256,9 @@
252256
<Trigger Property="IsEnabled" Value="false">
253257
<Setter Property="Opacity" Value="0.23"/>
254258
</Trigger>
259+
<Trigger Property="IsMouseOver" Value="True">
260+
<Setter TargetName="border" Property="wpf:ShadowAssist.Darken" Value="True" />
261+
</Trigger>
255262
</ControlTemplate.Triggers>
256263
</ControlTemplate>
257264
</Setter.Value>

0 commit comments

Comments
 (0)