diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8836.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8836.cs new file mode 100644 index 000000000..be16e4e26 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue8836.cs @@ -0,0 +1,48 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Github, 8836, "[Bug] ClearButtonVisibility.Never does not take effect on UWP", PlatformAffected.UWP)] + public class Issue8836 : TestContentPage + { + protected override void Init() + { + var stack = new StackLayout(); + + stack.Children.Add(new Label {Text = "Click the button to toggle the clear button visibility."}); + + Entry = new Entry + { + Text = "Clear Button: While Editing", + ClearButtonVisibility = ClearButtonVisibility.WhileEditing + }; + stack.Children.Add(Entry); + + var button = new Button { Text = "Toggle Clear Button State" }; + button.Clicked += Button_Clicked; + stack.Children.Add(button); + + Content = stack; + } + + private void Button_Clicked(object sender, System.EventArgs e) + { + if (Entry.ClearButtonVisibility == ClearButtonVisibility.Never) + { + Entry.ClearButtonVisibility = ClearButtonVisibility.WhileEditing; + Entry.Text = "Clear Button: While Editing"; + } + else + { + Entry.ClearButtonVisibility = ClearButtonVisibility.Never; + Entry.Text = "Clear Button: Never"; + } + Entry.Focus(); + } + + public Entry Entry { get; set; } + + } +} diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index cd9d61368..8482f0e9d 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -44,6 +44,7 @@ + Issue8902.xaml Code diff --git a/Xamarin.Forms.Platform.UAP/EntryRenderer.cs b/Xamarin.Forms.Platform.UAP/EntryRenderer.cs index 3dd36e7b7..8ec8c49c7 100644 --- a/Xamarin.Forms.Platform.UAP/EntryRenderer.cs +++ b/Xamarin.Forms.Platform.UAP/EntryRenderer.cs @@ -66,6 +66,9 @@ namespace Xamarin.Forms.Platform.UWP UpdateReturnType(); UpdateIsReadOnly(); UpdateInputScope(); + UpdateClearButtonVisibility(); + + if (_cursorPositionChangePending) UpdateCursorPosition(); @@ -148,6 +151,8 @@ namespace Xamarin.Forms.Platform.UWP UpdateSelectionLength(); else if (e.PropertyName == InputView.IsReadOnlyProperty.PropertyName) UpdateIsReadOnly(); + else if (e.PropertyName == Entry.ClearButtonVisibilityProperty.PropertyName) + UpdateClearButtonVisibility(); } protected override void UpdateBackgroundColor() @@ -236,6 +241,11 @@ namespace Xamarin.Forms.Platform.UWP Control.CharacterSpacing = Element.CharacterSpacing.ToEm(); } + void UpdateClearButtonVisibility() + { + Control.ClearButtonVisible = Element.ClearButtonVisibility == ClearButtonVisibility.WhileEditing; + } + void UpdateInputScope() { Entry entry = Element; diff --git a/Xamarin.Forms.Platform.UAP/FormsTextBox.cs b/Xamarin.Forms.Platform.UAP/FormsTextBox.cs index ac0471aa9..d3ed29065 100644 --- a/Xamarin.Forms.Platform.UAP/FormsTextBox.cs +++ b/Xamarin.Forms.Platform.UAP/FormsTextBox.cs @@ -42,9 +42,15 @@ namespace Xamarin.Forms.Platform.UWP public new static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(FormsTextBox), new PropertyMetadata("", TextPropertyChanged)); + public static readonly DependencyProperty ClearButtonVisibleProperty = DependencyProperty.Register(nameof(ClearButtonVisible), + typeof(bool), typeof(FormsTextBox), new PropertyMetadata(true, ClearButtonVisibleChanged)); + InputScope _passwordInputScope; InputScope _numericPasswordInputScope; Border _borderElement; + Windows.UI.Xaml.Controls.Grid _rootGrid; + Windows.UI.Xaml.VisualState _DeleteButtonVisibleState; + Windows.UI.Xaml.VisualStateGroup _DeleteButtonVisibleStateGroups; InputScope _cachedInputScope; bool _cachedPredictionsSetting; bool _cachedSpellCheckSetting; @@ -64,6 +70,12 @@ namespace Xamarin.Forms.Platform.UWP UpdateEnabled(); } + public bool ClearButtonVisible + { + get { return (bool)GetValue(ClearButtonVisibleProperty); } + set { SetValue(ClearButtonVisibleProperty, value);} + } + public Brush BackgroundFocusBrush { get { return (Brush)GetValue(BackgroundFocusBrushProperty); } @@ -146,6 +158,15 @@ namespace Xamarin.Forms.Platform.UWP // so we can manually handle its background when focused _borderElement = (Border)GetTemplateChild("BorderElement"); } + + _rootGrid = (Windows.UI.Xaml.Controls.Grid)GetTemplateChild("RootGrid"); + if (_rootGrid != null) + { + var stateGroups = WVisualStateManager.GetVisualStateGroups(_rootGrid).ToList(); + _DeleteButtonVisibleStateGroups = stateGroups.SingleOrDefault(sg => sg.Name == "ButtonStates"); + if (_DeleteButtonVisibleStateGroups != null) + _DeleteButtonVisibleState = _DeleteButtonVisibleStateGroups.States.SingleOrDefault(s => s.Name == "ButtonVisible"); + } } void DelayObfuscation() @@ -346,6 +367,21 @@ namespace Xamarin.Forms.Platform.UWP SelectionStart = base.Text.Length; } + static void ClearButtonVisibleChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) + { + var textBox = (FormsTextBox)dependencyObject; + var visibleState = textBox._DeleteButtonVisibleState; + var states = textBox._DeleteButtonVisibleStateGroups?.States; + + if (states != null && visibleState != null) + { + if (textBox.ClearButtonVisible && !states.Contains(visibleState)) + states.Add(visibleState); + else + states.Remove(visibleState); + } + } + static void TextPropertyChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs) { var textBox = (FormsTextBox)dependencyObject; diff --git a/Xamarin.Forms.Platform.UAP/FormsTextBoxStyle.xaml b/Xamarin.Forms.Platform.UAP/FormsTextBoxStyle.xaml index 2dc6282fa..504fbc0d3 100644 --- a/Xamarin.Forms.Platform.UAP/FormsTextBoxStyle.xaml +++ b/Xamarin.Forms.Platform.UAP/FormsTextBoxStyle.xaml @@ -26,7 +26,7 @@ - +