Click here to Skip to main content
15,896,915 members
Articles / Desktop Programming / WPF

Smart WPF Login Overlay (Windows 8 Style)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (23 votes)
3 Dec 2012CPOL12 min read 135.4K   18.1K   76  
Login Overlay for WPF applications with a styling similar to the Windows 8 Login Screen.
//	--------------------------------------------------------------------
//		Member of the WPFSmartLibrary
//		For more information see : http://wpfsmartlibrary.codeplex.com/
//		(by DotNetMastermind)
//
//		filename		: PasswordBoxBinding.cs
//		namespace	: SoftArcs.WPFSmartLibrary.UIClassAttachedProperties
//		class(es)	: PasswordBoxBinding
//							
//	--------------------------------------------------------------------
using System;
using System.Security;
using System.Windows;
using System.Windows.Controls;

namespace SoftArcs.WPFSmartLibrary.UIClassAttachedProperties
{
	public class PasswordBoxBinding
	{
		#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( PasswordBoxBinding ),
																	 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
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect SoftArcs
Germany Germany
Experience:
More than 20 years with software design, architecture and development. More than 9 years with the .NET Framework.

Preferences:
C#, .NET 2.0, .NET 3.5, .NET 4.0, .NET 4.5, WPF (3, 4 and 4.5), MVVM, XAML, Silverlight, Windows Phone, Windows 8 Apps, ASP.NET, WCF, T-SQL and especially GUI-Development and GUI-Design.

I would say I am a dotNet Developer with a keen affinity for developing and enriching user interfaces.

Comments and Discussions