Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide username on LoginDialog #2230

Merged
merged 3 commits into from
Dec 5, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion 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 DefaultShouldHideUsername = false;
private const bool DefaultEnablePasswordPreview = false;

public LoginDialogSettings()
{
UsernameWatermark = DefaultUsernameWatermark;
PasswordWatermark = DefaultPasswordWatermark;
NegativeButtonVisibility = DefaultNegativeButtonVisibility;
ShouldHideUsername = DefaultShouldHideUsername;
AffirmativeButtonText = "Login";
EnablePasswordPreview = DefaultEnablePasswordPreview;
}
Expand All @@ -27,6 +29,8 @@ public LoginDialogSettings()

public string UsernameWatermark { get; set; }

public bool ShouldHideUsername { get; set; }

public string PasswordWatermark { get; set; }

public Visibility NegativeButtonVisibility { get; set; }
Expand Down Expand Up @@ -56,14 +60,15 @@ internal LoginDialog(MetroWindow parentWindow, LoginDialogSettings settings)
UsernameWatermark = settings.UsernameWatermark;
PasswordWatermark = settings.PasswordWatermark;
NegativeButtonButtonVisibility = settings.NegativeButtonVisibility;
ShouldHideUsername = settings.ShouldHideUsername;
}

internal Task<LoginDialogData> WaitForButtonPressAsync()
{
Dispatcher.BeginInvoke(new Action(() =>
{
this.Focus();
if (string.IsNullOrEmpty(PART_TextBox.Text))
if (string.IsNullOrEmpty(PART_TextBox.Text) && !ShouldHideUsername)
{
PART_TextBox.Focus();
}
Expand Down Expand Up @@ -200,6 +205,7 @@ protected override void OnLoaded()
public static readonly DependencyProperty AffirmativeButtonTextProperty = DependencyProperty.Register("AffirmativeButtonText", typeof(string), typeof(LoginDialog), new PropertyMetadata("OK"));
public static readonly DependencyProperty NegativeButtonTextProperty = DependencyProperty.Register("NegativeButtonText", typeof(string), typeof(LoginDialog), new PropertyMetadata("Cancel"));
public static readonly DependencyProperty NegativeButtonButtonVisibilityProperty = DependencyProperty.Register("NegativeButtonButtonVisibility", typeof(Visibility), typeof(LoginDialog), new PropertyMetadata(Visibility.Collapsed));
public static readonly DependencyProperty ShouldHideUsernameProperty = DependencyProperty.Register("ShouldHideUsername", typeof(bool), typeof(LoginDialog), new PropertyMetadata(false));

public string Message
{
Expand Down Expand Up @@ -248,5 +254,11 @@ public Visibility NegativeButtonButtonVisibility
get { return (Visibility)GetValue(NegativeButtonButtonVisibilityProperty); }
set { SetValue(NegativeButtonButtonVisibilityProperty, value); }
}

public bool ShouldHideUsername
{
get { return (bool)GetValue(ShouldHideUsernameProperty); }
set { SetValue(ShouldHideUsernameProperty, value); }
}
}
}
44 changes: 44 additions & 0 deletions MahApps.Metro/Converters/InvertedBooleanToVisibilityConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;

namespace MahApps.Metro.Converters
{
public class InvertedBooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var shouldBeCollapsed = false;

if(value is bool)
{
shouldBeCollapsed = (bool)value;
}
else if(value is bool?)
{
var valueAsNullable = (bool?)value;
shouldBeCollapsed = valueAsNullable.HasValue ? valueAsNullable.Value : false;
}

return shouldBeCollapsed ? Visibility.Collapsed : Visibility.Hidden;


}

public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value is Visibility)
{
return (Visibility)value == Visibility.Collapsed;
}
else
{
return false;
}
}
}
}
1 change: 1 addition & 0 deletions MahApps.Metro/MahApps.Metro.NET45.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
<Compile Include="Controls\WindowCommandsOverlayBehavior.cs" />
<Compile Include="Converters\BackgroundToForegroundConverter.cs" />
<Compile Include="Converters\FontSizeOffsetConverter.cs" />
<Compile Include="Converters\InvertedBooleanToVisibilityConverter.cs" />
<Compile Include="Converters\IsNullConverter.cs" />
<Compile Include="Converters\MetroTabItemCloseButtonWidthConverter.cs" />
<Compile Include="Converters\OffOnConverter.cs" />
Expand Down
1 change: 1 addition & 0 deletions MahApps.Metro/MahApps.Metro.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@
</Compile>
<Compile Include="Controls\WindowCommandsOverlayBehavior.cs" />
<Compile Include="Converters\BackgroundToForegroundConverter.cs" />
<Compile Include="Converters\InvertedBooleanToVisibilityConverter.cs" />
<Compile Include="Converters\ThicknessBindingConverter.cs" />
<Compile Include="Converters\IsNullConverter.cs" />
<Compile Include="Converters\FontSizeOffsetConverter.cs" />
Expand Down
5 changes: 5 additions & 0 deletions MahApps.Metro/Themes/Dialogs/LoginDialog.xaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
<Dialogs:BaseMetroDialog xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:Controls="clr-namespace:MahApps.Metro.Controls"
xmlns:Converters="clr-namespace:MahApps.Metro.Converters"
xmlns:Dialogs="clr-namespace:MahApps.Metro.Controls.Dialogs"
xmlns:Behaviors="clr-namespace:MahApps.Metro.Behaviours"
x:Class="MahApps.Metro.Controls.Dialogs.LoginDialog">
<Dialogs:BaseMetroDialog.Resources>
<Converters:InvertedBooleanToVisibilityConverter x:Key="InvertedBooleanToVisibilityConverter"/>
</Dialogs:BaseMetroDialog.Resources>
<Grid Margin="0 10 0 0">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"
Expand All @@ -26,6 +30,7 @@
Controls:TextBoxHelper.SelectAllOnFocus="True"
Text="{Binding Username, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Visibility="{Binding ShouldHideUsername, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged, Converter={StaticResource InvertedBooleanToVisibilityConverter}}"
Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}" />
<PasswordBox Grid.Row="2"
Margin="0 5 0 0"
Expand Down
2 changes: 2 additions & 0 deletions samples/MetroDemo/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@
Header="Show LoginDialog" />
<MenuItem Click="ShowLoginDialogPasswordPreview"
Header="Show Password Preview LoginDialog" />
<MenuItem Click="ShowLoginDialogOnlyPassword"
Header="Show LoginDialog (Only Password)" />
<MenuItem Click="ShowMessageDialog"
Header="Show MessageDialog" />
<MenuItem Click="ShowLimitedMessageDialog"
Expand Down
16 changes: 15 additions & 1 deletion samples/MetroDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ private async void CloseCustomDialog(object sender, RoutedEventArgs e)
await this.HideMetroDialogAsync(dialog);
}

private async void ShowLoginDialogPasswordPreview(object sender, RoutedEventArgs e)
private async void ShowLoginDialogPasswordPreview(object sender, RoutedEventArgs e)
{
LoginDialogData result = await this.ShowLoginAsync("Authentication", "Enter your credentials", new LoginDialogSettings { ColorScheme = this.MetroDialogOptions.ColorScheme, InitialUsername = "MahApps", EnablePasswordPreview = true });
if (result == null)
Expand All @@ -243,6 +243,20 @@ private async void ShowLoginDialogPasswordPreview(object sender, RoutedEventArgs
MessageDialogResult messageResult = await this.ShowMessageAsync("Authentication Information", String.Format("Username: {0}\nPassword: {1}", result.Username, result.Password));
}
}

private async void ShowLoginDialogOnlyPassword(object sender, RoutedEventArgs e)
{
LoginDialogData result = await this.ShowLoginAsync("Authentication", "Enter your password", new LoginDialogSettings { ColorScheme = this.MetroDialogOptions.ColorScheme, ShouldHideUsername = true });
if (result == null)
{
//User pressed cancel
}
else
{
MessageDialogResult messageResult = await this.ShowMessageAsync("Authentication Information", String.Format("Password: {0}", result.Password));
}
}

private async void ShowProgressDialog(object sender, RoutedEventArgs e)
{
var controller = await this.ShowProgressAsync("Please wait...", "We are baking some cupcakes!");
Expand Down