Skip to content

Commit 76b4853

Browse files
committed
allow underline animation to be switched off #137
1 parent f9b330e commit 76b4853

File tree

3 files changed

+75
-29
lines changed

3 files changed

+75
-29
lines changed

MainDemo.Wpf/TextFields.xaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@
8888
</Canvas>
8989
</Viewbox>
9090
<TextBox Grid.Row="1" Grid.Column="1"
91-
x:Name="PhoneTextBox"
92-
materialDesign:TextFieldAssist.Hint="Phone"
91+
x:Name="PhoneTextBox"
92+
materialDesign:TransitionAssist.DisableTransitions="True"
9393
/>
9494
<TextBlock Grid.Row="1" Grid.Column="2" VerticalAlignment="Center" Margin="16 0 8 0">Fruit</TextBlock>
9595
<ComboBox Grid.Row="1" Grid.Column="3" materialDesign:TextFieldAssist.Hint="Search"

MaterialDesignThemes.Wpf/Themes/Generic.xaml

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -155,20 +155,6 @@
155155
<ControlTemplate>
156156
<ControlTemplate.Resources>
157157
<CircleEase x:Key="UnderlineEasingFunction" EasingMode="EaseOut"/>
158-
<Storyboard x:Key="ExpandUnderlineBorderStoryboard">
159-
<DoubleAnimation Storyboard.TargetName="UnderlineBorder"
160-
Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)"
161-
Duration="0:0:0.3"
162-
EasingFunction="{StaticResource UnderlineEasingFunction}"
163-
To="1"/>
164-
</Storyboard>
165-
<Storyboard x:Key="CollapseUnderlineBorderStoryboard">
166-
<DoubleAnimation Storyboard.TargetName="UnderlineBorder"
167-
Storyboard.TargetProperty="(UIElement.RenderTransform).(ScaleTransform.ScaleX)"
168-
Duration="0:0:0.3"
169-
EasingFunction="{StaticResource UnderlineEasingFunction}"
170-
To="0"/>
171-
</Storyboard>
172158
</ControlTemplate.Resources>
173159
<Border x:Name="UnderlineBorder"
174160
Background="{TemplateBinding Background}"
@@ -178,20 +164,50 @@
178164
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
179165
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
180166
RenderTransformOrigin="0.5,0.5">
167+
<VisualStateManager.VisualStateGroups>
168+
<VisualStateGroup x:Name="ActivationStates">
169+
<VisualStateGroup.Transitions>
170+
<VisualTransition From="Inactive" To="Active">
171+
<Storyboard>
172+
<DoubleAnimation Storyboard.TargetName="ScaleTransform"
173+
Storyboard.TargetProperty="ScaleX"
174+
Duration="0:0:0.3"
175+
EasingFunction="{StaticResource UnderlineEasingFunction}"
176+
To="1"/>
177+
</Storyboard>
178+
</VisualTransition>
179+
<VisualTransition From="Active" To="Inactive">
180+
<Storyboard>
181+
<DoubleAnimation Storyboard.TargetName="ScaleTransform"
182+
Storyboard.TargetProperty="ScaleX"
183+
Duration="0:0:0.3"
184+
EasingFunction="{StaticResource UnderlineEasingFunction}"
185+
To="0"/>
186+
</Storyboard>
187+
</VisualTransition>
188+
</VisualStateGroup.Transitions>
189+
<VisualState x:Name="Active">
190+
<Storyboard>
191+
<DoubleAnimation Storyboard.TargetName="ScaleTransform"
192+
Storyboard.TargetProperty="ScaleX"
193+
Duration="0"
194+
To="1"/>
195+
</Storyboard>
196+
</VisualState>
197+
<VisualState x:Name="Inactive">
198+
<Storyboard>
199+
<DoubleAnimation Storyboard.TargetName="ScaleTransform"
200+
Storyboard.TargetProperty="ScaleX"
201+
Duration="0"
202+
To="0"/>
203+
</Storyboard>
204+
</VisualState>
205+
</VisualStateGroup>
206+
</VisualStateManager.VisualStateGroups>
181207
<Border.RenderTransform>
182-
<ScaleTransform ScaleX="0" ScaleY="1"/>
208+
<ScaleTransform ScaleX="0" ScaleY="1" x:Name="ScaleTransform" />
183209
</Border.RenderTransform>
184210
</Border>
185-
<ControlTemplate.Triggers>
186-
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=IsActive}" Value="True">
187-
<DataTrigger.EnterActions>
188-
<BeginStoryboard Storyboard="{StaticResource ExpandUnderlineBorderStoryboard}"/>
189-
</DataTrigger.EnterActions>
190-
<DataTrigger.ExitActions>
191-
<BeginStoryboard Storyboard="{StaticResource CollapseUnderlineBorderStoryboard}"/>
192-
</DataTrigger.ExitActions>
193-
</DataTrigger>
194-
</ControlTemplate.Triggers>
195211
</ControlTemplate>
196212
</Setter.Value>
197213
</Setter>

MaterialDesignThemes.Wpf/Underline.cs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,51 @@
99

1010
namespace MaterialDesignThemes.Wpf
1111
{
12+
/// <summary>
13+
///
14+
/// </summary>
15+
[TemplateVisualState(GroupName="ActivationStates", Name = ActiveStateName)]
16+
[TemplateVisualState(GroupName = "ActivationStates", Name = InactiveStateName)]
1217
public class Underline : Control
1318
{
19+
public const string ActiveStateName = "Active";
20+
public const string InactiveStateName = "Inactive";
21+
1422
static Underline()
1523
{
1624
DefaultStyleKeyProperty.OverrideMetadata(typeof(Underline), new FrameworkPropertyMetadata(typeof(Underline)));
1725
}
1826

1927
public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(
2028
"IsActive", typeof(bool), typeof(Underline),
21-
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender));
29+
new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.AffectsRender, IsActivePropertyChangedCallback));
30+
31+
private static void IsActivePropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
32+
{
33+
((Underline)dependencyObject).GotoVisualState(!TransitionAssist.GetDisableTransitions(dependencyObject));
34+
}
2235

2336
public bool IsActive
2437
{
2538
get { return (bool)GetValue(IsActiveProperty); }
2639
set { SetValue(IsActiveProperty, value); }
27-
}
40+
}
41+
42+
public override void OnApplyTemplate()
43+
{
44+
base.OnApplyTemplate();
45+
46+
GotoVisualState(false);
47+
}
48+
49+
private void GotoVisualState(bool useTransitions)
50+
{
51+
VisualStateManager.GoToState(this, SelectStateName(), useTransitions);
52+
}
53+
54+
private string SelectStateName()
55+
{
56+
return IsActive ? ActiveStateName : InactiveStateName;
57+
}
2858
}
2959
}

0 commit comments

Comments
 (0)