Skip to content

Commit

Permalink
Merge pull request #1873 from bitbeans/master
Browse files Browse the repository at this point in the history
New try for LoginDialog Password Preview Feature
  • Loading branch information
punker76 committed Apr 23, 2015
2 parents ed0ce24 + 87b80a9 commit 5999b37
Show file tree
Hide file tree
Showing 9 changed files with 656 additions and 0 deletions.
72 changes: 72 additions & 0 deletions MahApps.Metro/Behaviours/PasswordBoxBindingBehavior.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// --------------------------------------------------------------------
// Obtained from: WPFSmartLibrary
// For more information see : http://wpfsmartlibrary.codeplex.com/
// (by DotNetMastermind)
// --------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Interactivity;

namespace MahApps.Metro.Behaviours
{
public class PasswordBoxBindingBehavior : Behavior<PasswordBox>
{
#region Fields

private static bool IsUpdating;

#endregion

#region DependencyProperty - Password ("string")

public static string GetPassword(DependencyObject dpo)
{
return (string)dpo.GetValue(PasswordProperty);
}
public static void SetPassword(DependencyObject dpo, string value)
{
dpo.SetValue(PasswordProperty, value);
}
/// <summary>
/// Gets or sets the bindable Password property on the PasswordBox control. This is a dependency property.
/// </summary>
public static readonly DependencyProperty PasswordProperty =
DependencyProperty.RegisterAttached("Password", typeof(string),
typeof(PasswordBoxBindingBehavior),
new FrameworkPropertyMetadata(String.Empty,
new PropertyChangedCallback(OnPasswordPropertyChanged)));
/// <summary>
/// Handles changes to the 'Password' attached property.
/// </summary>
private static void OnPasswordPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
PasswordBox targetPasswordBox = sender as PasswordBox;
if (targetPasswordBox != null)
{
targetPasswordBox.PasswordChanged -= PasswordBox_PasswordChanged;
if (IsUpdating == false)
{
targetPasswordBox.Password = (string)e.NewValue;
}
targetPasswordBox.PasswordChanged += PasswordBox_PasswordChanged;
}
}

/// <summary>
/// Handle the 'PasswordChanged'-event on the PasswordBox
/// </summary>
private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
{
PasswordBox passwordBox = sender as PasswordBox;
IsUpdating = true;
SetPassword(passwordBox, passwordBox.Password);
IsUpdating = false;
}

#endregion
}
}
12 changes: 12 additions & 0 deletions MahApps.Metro/Controls/Dialogs/LoginDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ public class LoginDialogSettings : MetroDialogSettings
private const string DefaultUsernameWatermark = "Username...";
private const string DefaultPasswordWatermark = "Password...";
private const Visibility DefaultNegativeButtonVisibility = Visibility.Collapsed;
private const bool DefaultEnablePasswordPreview = false;

public LoginDialogSettings()
{
UsernameWatermark = DefaultUsernameWatermark;
PasswordWatermark = DefaultPasswordWatermark;
NegativeButtonVisibility = DefaultNegativeButtonVisibility;
AffirmativeButtonText = "Login";
EnablePasswordPreview = DefaultEnablePasswordPreview;
}

public string InitialUsername { get; set; }
Expand All @@ -26,6 +28,8 @@ public LoginDialogSettings()
public string PasswordWatermark { get; set; }

public Visibility NegativeButtonVisibility { get; set; }

public bool EnablePasswordPreview { get; set; }
}

public class LoginDialogData
Expand All @@ -49,6 +53,14 @@ internal LoginDialog(MetroWindow parentWindow, LoginDialogSettings settings)
UsernameWatermark = settings.UsernameWatermark;
PasswordWatermark = settings.PasswordWatermark;
NegativeButtonButtonVisibility = settings.NegativeButtonVisibility;
if (settings.EnablePasswordPreview)
{
object resource = Application.Current.FindResource("Win8MetroPasswordBox");
if (resource != null && resource.GetType() == typeof(Style))
{
PART_TextBox2.Style = (Style)resource;
}
}
}

internal Task<LoginDialogData> WaitForButtonPressAsync()
Expand Down
85 changes: 85 additions & 0 deletions MahApps.Metro/Converters/StringToVisibilityConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// --------------------------------------------------------------------
// Obtained from: WPFSmartLibrary
// For more information see : http://wpfsmartlibrary.codeplex.com/
// (by DotNetMastermind)
// --------------------------------------------------------------------

using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;

namespace MahApps.Metro.Converters
{
/// <summary>
/// Converts a String into a Visibility enumeration (and back)
/// The FalseEquivalent can be declared with the "FalseEquivalent" property
/// </summary>
[ValueConversion(typeof (string), typeof (Visibility))]
[MarkupExtensionReturnType(typeof (StringToVisibilityConverter))]
public class StringToVisibilityConverter : MarkupExtension, IValueConverter
{
/// <summary>
/// Initialize the properties with standard values
/// </summary>
public StringToVisibilityConverter()
{
FalseEquivalent = Visibility.Collapsed;
OppositeStringValue = false;
}

/// <summary>
/// FalseEquivalent (default : Visibility.Collapsed => see Constructor)
/// </summary>
public Visibility FalseEquivalent { get; set; }

/// <summary>
/// Define whether the opposite boolean value is crucial (default : false)
/// </summary>
public bool OppositeStringValue { get; set; }

#region MarkupExtension "overrides"

public override object ProvideValue(IServiceProvider serviceProvider)
{
return new StringToVisibilityConverter
{
FalseEquivalent = FalseEquivalent,
OppositeStringValue = OppositeStringValue
};
}

#endregion

#region IValueConverter Members

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is string && targetType == typeof (Visibility))
{
if (OppositeStringValue)
{
return (((string) value).ToLower().Equals(String.Empty)) ? Visibility.Visible : FalseEquivalent;
}
return (((string) value).ToLower().Equals(String.Empty)) ? FalseEquivalent : Visibility.Visible;
}
return value;
}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility)
{
if (OppositeStringValue)
{
return ((Visibility) value == Visibility.Visible) ? String.Empty : "visible";
}
return ((Visibility) value == Visibility.Visible) ? "visible" : String.Empty;
}
return value;
}

#endregion
}
}
2 changes: 2 additions & 0 deletions MahApps.Metro/MahApps.Metro.NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@
<Compile Include="Behaviours\BindableResourceBehavior.cs" />
<Compile Include="Behaviours\BorderlessWindowBehavior.cs" />
<Compile Include="Behaviours\GlowWindowBehavior.cs" />
<Compile Include="Behaviours\PasswordBoxBindingBehavior.cs" />
<Compile Include="Behaviours\StylizedBehaviorCollection.cs" />
<Compile Include="Behaviours\TabControlSelectFirstVisibleTabBehavior.cs" />
<Compile Include="Behaviours\WindowsSettingBehaviour.cs" />
Expand Down Expand Up @@ -197,6 +198,7 @@
<Compile Include="Controls\VisualStates.cs" />
<Compile Include="Controls\WindowCommands.cs" />
<Compile Include="Converters\ResizeModeMinMaxButtonVisibilityConverter.cs" />
<Compile Include="Converters\StringToVisibilityConverter.cs" />
<Compile Include="Converters\ThicknessToDoubleConverter.cs" />
<Compile Include="Converters\TreeViewMarginConverter.cs" />
<Compile Include="Microsoft.Windows.Shell\Standard\ComGuids.cs" />
Expand Down
8 changes: 8 additions & 0 deletions MahApps.Metro/MahApps.Metro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
<Compile Include="Behaviours\BindableResourceBehavior.cs" />
<Compile Include="Behaviours\BorderlessWindowBehavior.cs" />
<Compile Include="Behaviours\GlowWindowBehavior.cs" />
<Compile Include="Behaviours\PasswordBoxBindingBehavior.cs" />
<Compile Include="Behaviours\StylizedBehaviorCollection.cs" />
<Compile Include="Behaviours\WindowsSettingBehaviour.cs" />
<Compile Include="Controls\ClosingWindowEventHandlerArgs.cs" />
Expand Down Expand Up @@ -159,6 +160,7 @@
<Compile Include="Controls\TreeHelper.cs" />
<Compile Include="Controls\VisualStates.cs" />
<Compile Include="Converters\ResizeModeMinMaxButtonVisibilityConverter.cs" />
<Compile Include="Converters\StringToVisibilityConverter.cs" />
<Compile Include="Converters\ThicknessToDoubleConverter.cs" />
<Compile Include="Converters\TreeViewMarginConverter.cs" />
<Compile Include="Microsoft.Windows.Shell\Standard\ComGuids.cs" />
Expand Down Expand Up @@ -678,6 +680,12 @@
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="MahApps.Metro.NET45.csproj">
<Project>{942D11C4-29F5-46C4-8C14-4B976D56C637}</Project>
<Name>MahApps.Metro.NET45</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
Expand Down
Loading

0 comments on commit 5999b37

Please sign in to comment.