Skip to content

Commit

Permalink
name validation fix, page navigate fix
Browse files Browse the repository at this point in the history
- fine-tune name validation and warning display
- also fixes issue when navigating away from the publish page and setting up the datacontext of the PackageManagerPublishControl
  • Loading branch information
dnenov committed Dec 8, 2023
1 parent 8987869 commit 2adf7b9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 3 deletions.
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

0 comments on commit 2adf7b9

Please sign in to comment.