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

Pm name validation fix, page navigate fix #14724

Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,43 @@ public PackageManagerPublishControl()
InitializeComponent();

this.Loaded += InitializeContext;
this.DataContextChanged += PackageManagerPublishControl_DataContextChanged;
}

private void PackageManagerPublishControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if (!(this.DataContext is PublishPackageViewModel)) return;

ResetDataContext();
SetDataContext();
}

private void InitializeContext(object sender, RoutedEventArgs e)
{
SetDataContext();

this.Loaded -= InitializeContext;
}

private void ResetDataContext()
{
if (PublishPackageViewModel != null)
{
PublishPackageViewModel.PublishSuccess -= PackageViewModelOnPublishSuccess;
PublishPackageViewModel.RequestShowFolderBrowserDialog -= OnRequestShowFolderBrowserDialog;
}

PublishPackageViewModel = null;
}
private void SetDataContext()
{
// Set the owner of this user control
this.Owner = Window.GetWindow(this);

PublishPackageViewModel = this.DataContext as PublishPackageViewModel;
PublishPackageViewModel.Owner = this.Owner;

if(PublishPackageViewModel != null )
if (PublishPackageViewModel != null)
{
PublishPackageViewModel.PublishSuccess += PackageViewModelOnPublishSuccess;
PublishPackageViewModel.RequestShowFolderBrowserDialog += OnRequestShowFolderBrowserDialog;
Expand Down Expand Up @@ -78,6 +104,8 @@ public void Dispose()
NavButtonStacks = null;

Breadcrumbs?.Clear();

this.DataContextChanged -= PackageManagerPublishControl_DataContextChanged;
}

private void InitializePages()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Navigation;

Expand All @@ -11,7 +12,7 @@ public class PackageNameLengthValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (value is string name && name.TrimEnd().Length > 2)
if (IsValidName((string)value))
{
// Validation succeeded
return ValidationResult.ValidResult;
Expand All @@ -20,6 +21,11 @@ public override ValidationResult Validate(object value, CultureInfo cultureInfo)
// Validation failed
return new ValidationResult(false, Wpf.Properties.Resources.NameNeedMoreCharacters);
}
public static bool IsValidName(string value)
{
return (value is string name && name.TrimEnd().Length > 2);
}

}


Expand Down Expand Up @@ -136,11 +142,36 @@ private void textBoxInput_PreviewKeyDown(object sender, System.Windows.Input.Key
{
var textBox = sender as TextBox;
if (textBox == null) return;
if (e.Key == Key.System) return;

int caretIndex = textBox.CaretIndex; // Store the caret index

// Prevents text starting with a space
if (e.Key == System.Windows.Input.Key.Space && string.IsNullOrWhiteSpace(textBox.Text))
{
e.Handled = true;
e.Handled = true;
return;
}

if (string.IsNullOrEmpty(textBox.Text)) { return; }

// In case we are using the Backspace to remove characters, the validation error will stop the Name property from being updated
if (textBox.Name.Equals("packageNameInput") && e.Key == Key.Back && !PackageNameLengthValidationRule.IsValidName(textBox.Text.Substring(0, textBox.Text.Length - 1)))
{
e.Handled = true;

if (!string.IsNullOrEmpty(PublishPackageViewModel.Name))
{
// Manually remove the last character from the Name property, as the validation error will not update the Name property
PublishPackageViewModel.Name = PublishPackageViewModel.Name.Substring(0, PublishPackageViewModel.Name.Length - 1);

// Trigger re-validation explicitly
var expression = textBox.GetBindingExpression(TextBox.TextProperty);
expression?.UpdateSource();

textBox.CaretIndex = caretIndex - 1 >= 0 ? caretIndex - 1 : 0;
return;
}
}
}
}
Expand Down
Loading