Skip to content

Commit

Permalink
Changed property type from Visibility to bool
Browse files Browse the repository at this point in the history
  • Loading branch information
mgnslndh committed Nov 27, 2015
1 parent 4453602 commit 5d6e1a3
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
18 changes: 9 additions & 9 deletions MahApps.Metro/Controls/Dialogs/LoginDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ public class LoginDialogSettings : MetroDialogSettings
private const string DefaultUsernameWatermark = "Username...";
private const string DefaultPasswordWatermark = "Password...";
private const Visibility DefaultNegativeButtonVisibility = Visibility.Collapsed;
private const Visibility DefaultUsernameVisibility = Visibility.Visible;
private const bool DefaultShouldHideUsername = false;
private const bool DefaultEnablePasswordPreview = false;

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

public string UsernameWatermark { get; set; }

public Visibility UsernameVisibility { get; set; }
public bool ShouldHideUsername { get; set; }

public string PasswordWatermark { get; set; }

Expand Down Expand Up @@ -60,15 +60,15 @@ internal LoginDialog(MetroWindow parentWindow, LoginDialogSettings settings)
UsernameWatermark = settings.UsernameWatermark;
PasswordWatermark = settings.PasswordWatermark;
NegativeButtonButtonVisibility = settings.NegativeButtonVisibility;
UsernameVisibility = settings.UsernameVisibility;
ShouldHideUsername = settings.ShouldHideUsername;
}

internal Task<LoginDialogData> WaitForButtonPressAsync()
{
Dispatcher.BeginInvoke(new Action(() =>
{
this.Focus();
if (string.IsNullOrEmpty(PART_TextBox.Text) && UsernameVisibility == Visibility.Visible)
if (string.IsNullOrEmpty(PART_TextBox.Text) && !ShouldHideUsername)
{
PART_TextBox.Focus();
}
Expand Down Expand Up @@ -205,7 +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 UsernameVisibilityProperty = DependencyProperty.Register("UsernameVisibility", typeof(Visibility), typeof(LoginDialog), new PropertyMetadata(Visibility.Visible));
public static readonly DependencyProperty ShouldHideUsernameProperty = DependencyProperty.Register("ShouldHideUsername", typeof(bool), typeof(LoginDialog), new PropertyMetadata(false));

public string Message
{
Expand Down Expand Up @@ -255,10 +255,10 @@ public Visibility NegativeButtonButtonVisibility
set { SetValue(NegativeButtonButtonVisibilityProperty, value); }
}

public Visibility UsernameVisibility
public bool ShouldHideUsername
{
get { return (Visibility)GetValue(UsernameVisibilityProperty); }
set { SetValue(UsernameVisibilityProperty, value); }
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
6 changes: 5 additions & 1 deletion 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,7 +30,7 @@
Controls:TextBoxHelper.SelectAllOnFocus="True"
Text="{Binding Username, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
TextWrapping="Wrap"
Visibility="{Binding UsernameVisibility, RelativeSource={RelativeSource AncestorType=Dialogs:LoginDialog, Mode=FindAncestor}, UpdateSourceTrigger=PropertyChanged}"
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: 1 addition & 1 deletion samples/MetroDemo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ private async void ShowLoginDialogPasswordPreview(object sender, RoutedEventArgs

private async void ShowLoginDialogOnlyPassword(object sender, RoutedEventArgs e)
{
LoginDialogData result = await this.ShowLoginAsync("Authentication", "Enter your password", new LoginDialogSettings { ColorScheme = this.MetroDialogOptions.ColorScheme, UsernameVisibility = Visibility.Collapsed });
LoginDialogData result = await this.ShowLoginAsync("Authentication", "Enter your password", new LoginDialogSettings { ColorScheme = this.MetroDialogOptions.ColorScheme, ShouldHideUsername = true });
if (result == null)
{
//User pressed cancel
Expand Down

0 comments on commit 5d6e1a3

Please sign in to comment.