Skip to content

Commit 4f90b74

Browse files
committed
Fix #82
1 parent 091caa9 commit 4f90b74

File tree

2 files changed

+63
-3
lines changed

2 files changed

+63
-3
lines changed

FluentWPF/Extensions.cs

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,67 @@ public static Brush GetPlaceholderForegroundBrush(UIElement element)
8181
return (Brush)element.GetValue(PlaceholderForegroundBrushProperty);
8282
}
8383

84-
}
8584

8685

8786

87+
#region PasswordBox Attached Prperties
88+
89+
internal static bool GetIsPasswordBoxExtensionAttached(DependencyObject obj)
90+
{
91+
return (bool)obj.GetValue(IsPasswordBoxExtensionAttachedProperty);
92+
}
93+
internal static void SetIsPasswordBoxExtensionAttached(DependencyObject obj, bool value)
94+
{
95+
obj.SetValue(IsPasswordBoxExtensionAttachedProperty, value);
96+
}
97+
// Using a DependencyProperty as the backing store for IsPasswordBoxExtensionAttached. This enables animation, styling, binding, etc...
98+
public static readonly DependencyProperty IsPasswordBoxExtensionAttachedProperty =
99+
DependencyProperty.RegisterAttached("IsPasswordBoxExtensionAttached", typeof(bool), typeof(Extensions), new PropertyMetadata(false, OnIsPasswordBoxExtensionAttachedChanged));
100+
101+
private static void OnIsPasswordBoxExtensionAttachedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
102+
{
103+
var ctrl = d as PasswordBox;
104+
var newValue = (bool)e.NewValue;
105+
var oldValue = (bool)e.OldValue;
106+
if (ctrl == null) return;
107+
108+
// 無効になった場合の処理
109+
if (oldValue && !newValue)
110+
{
111+
ctrl.PasswordChanged -= Ctrl_PasswordChanged;
112+
}
113+
114+
// 有効になった場合の処理
115+
if (!oldValue && newValue)
116+
{
117+
ctrl.PasswordChanged += Ctrl_PasswordChanged;
118+
var value = string.IsNullOrEmpty(ctrl.Password);
119+
SetIsPasswordEmpty(ctrl, value);
120+
}
121+
}
122+
123+
private static void Ctrl_PasswordChanged(object sender, RoutedEventArgs e)
124+
{
125+
var ctrl = sender as PasswordBox;
126+
if (ctrl != null)
127+
{
128+
var value = string.IsNullOrEmpty(ctrl.Password);
129+
SetIsPasswordEmpty(ctrl, value);
130+
}
131+
}
132+
133+
public static bool GetIsPasswordEmpty(DependencyObject obj)
134+
{
135+
return (bool)obj.GetValue(IsPasswordEmptyProperty);
136+
}
137+
private static void SetIsPasswordEmpty(DependencyObject obj, bool value)
138+
{
139+
obj.SetValue(IsPasswordEmptyProperty, value);
140+
}
141+
// Using a DependencyProperty as the backing store for IsPasswordEmpty. This enables animation, styling, binding, etc...
142+
public static readonly DependencyProperty IsPasswordEmptyProperty =
143+
DependencyProperty.RegisterAttached("IsPasswordEmpty", typeof(bool), typeof(Extensions), new PropertyMetadata(true));
144+
145+
#endregion
146+
}
88147
}

FluentWPF/Styles/TextBox.xaml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,15 @@
145145
<Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
146146
<Setter Property="Padding" Value="5"/>
147147
<Setter Property="FontSize" Value="14"/>
148+
<Setter Property="local:Extensions.IsPasswordBoxExtensionAttached" Value="True"/>
148149
<Setter Property="Template">
149150
<Setter.Value>
150151
<ControlTemplate TargetType="{x:Type PasswordBox}">
151152
<ControlTemplate.Resources>
152153
<Storyboard x:Key="enterGotFocus">
153154
<DoubleAnimation Storyboard.TargetName="TextBlock_PlaceHolder"
154155
Storyboard.TargetProperty="Opacity"
155-
To=".0"
156+
To=".2"
156157
Duration="0:0:0.2" />
157158
</Storyboard>
158159
<Storyboard x:Key="exitGotFocus">
@@ -225,7 +226,7 @@
225226
</DataTrigger>
226227

227228

228-
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=Password, Mode=OneWay}" Value="">
229+
<DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=(local:Extensions.IsPasswordEmpty), Mode=OneWay}" Value="True">
229230
<DataTrigger.EnterActions>
230231
<BeginStoryboard Storyboard="{StaticResource exitHasText}" />
231232
</DataTrigger.EnterActions>

0 commit comments

Comments
 (0)