From 1079966b138320ae556790e459c64eae4a7371ae Mon Sep 17 00:00:00 2001 From: Shane Neuville Date: Mon, 31 Jul 2023 14:34:36 -0500 Subject: [PATCH] Fix EntryCellRenderer to use fromhandler for text changes --- .../src/Android/Cells/EntryCellRenderer.cs | 3 +- .../Core/src/iOS/Cells/EntryCellRenderer.cs | 3 +- .../ListView/Android/EntryCellRenderer.cs | 4 +- .../ListView/iOS/EntryCellRenderer.cs | 3 +- .../Elements/ListView/ListViewTests.cs | 61 ++++++++++++++++++- 5 files changed, 69 insertions(+), 5 deletions(-) diff --git a/src/Compatibility/Core/src/Android/Cells/EntryCellRenderer.cs b/src/Compatibility/Core/src/Android/Cells/EntryCellRenderer.cs index 429e9c9a4585..5ef699044f44 100644 --- a/src/Compatibility/Core/src/Android/Cells/EntryCellRenderer.cs +++ b/src/Compatibility/Core/src/Android/Cells/EntryCellRenderer.cs @@ -92,7 +92,8 @@ void OnEditingCompleted() void OnTextChanged(string text) { var entryCell = (EntryCell)Cell; - entryCell.Text = text; + entryCell + .SetValue(EntryCell.TextProperty, text, specificity: SetterSpecificity.FromHandler); } void UpdateHeight() diff --git a/src/Compatibility/Core/src/iOS/Cells/EntryCellRenderer.cs b/src/Compatibility/Core/src/iOS/Cells/EntryCellRenderer.cs index eb6a6dd65a7b..7602045a5deb 100644 --- a/src/Compatibility/Core/src/iOS/Cells/EntryCellRenderer.cs +++ b/src/Compatibility/Core/src/iOS/Cells/EntryCellRenderer.cs @@ -90,7 +90,8 @@ static void OnTextFieldTextChanged(object sender, EventArgs eventArgs) var cell = (EntryCellTableViewCell)sender; var model = (EntryCell)cell.Cell; - model.Text = cell.TextField.Text; + model + .SetValue(EntryCell.TextProperty, cell.TextField.Text, specificity: SetterSpecificity.FromHandler); } static void UpdateHorizontalTextAlignment(EntryCellTableViewCell cell, EntryCell entryCell) diff --git a/src/Controls/src/Core/Compatibility/Handlers/ListView/Android/EntryCellRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/ListView/Android/EntryCellRenderer.cs index 0eb67b36efc6..e624f4bdba3a 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/ListView/Android/EntryCellRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/ListView/Android/EntryCellRenderer.cs @@ -90,7 +90,9 @@ void OnEditingCompleted() void OnTextChanged(string text) { var entryCell = (EntryCell)Cell; - entryCell.Text = text; + + entryCell + .SetValue(EntryCell.TextProperty, text, specificity: SetterSpecificity.FromHandler); } void UpdateHeight() diff --git a/src/Controls/src/Core/Compatibility/Handlers/ListView/iOS/EntryCellRenderer.cs b/src/Controls/src/Core/Compatibility/Handlers/ListView/iOS/EntryCellRenderer.cs index 4ec82b41fa40..0a6ff189c91d 100644 --- a/src/Controls/src/Core/Compatibility/Handlers/ListView/iOS/EntryCellRenderer.cs +++ b/src/Controls/src/Core/Compatibility/Handlers/ListView/iOS/EntryCellRenderer.cs @@ -95,7 +95,8 @@ static void OnTextFieldTextChanged(object sender, EventArgs eventArgs) var cell = (EntryCellTableViewCell)sender; var model = (EntryCell)cell.Cell; - model.Text = cell.TextField.Text; + model + .SetValue(EntryCell.TextProperty, cell.TextField.Text, specificity: SetterSpecificity.FromHandler); } static void UpdateHorizontalTextAlignment(EntryCellTableViewCell cell, EntryCell entryCell) diff --git a/src/Controls/tests/DeviceTests/Elements/ListView/ListViewTests.cs b/src/Controls/tests/DeviceTests/Elements/ListView/ListViewTests.cs index 2958f0f0a305..219afdc4b270 100644 --- a/src/Controls/tests/DeviceTests/Elements/ListView/ListViewTests.cs +++ b/src/Controls/tests/DeviceTests/Elements/ListView/ListViewTests.cs @@ -1,4 +1,6 @@ -using System.Collections.ObjectModel; +using System; +using System.Collections.ObjectModel; +using System.ComponentModel; using System.Linq; using System.Threading.Tasks; using Microsoft.Maui.Controls; @@ -19,6 +21,7 @@ void SetupBuilder() { builder.ConfigureMauiHandlers(handlers => { + handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); handlers.AddHandler(); @@ -113,6 +116,62 @@ await CreateHandlerAndAddToWindow(layout, async (handler) => }); } + [Fact] + public async Task EntryCellBindingCorrectlyUpdates() + { + SetupBuilder(); + var vm = new EntryCellBindingCorrectlyUpdatesVM(); + var data = new[] + { + vm + }; + + var listView = new ListView() + { + ItemTemplate = new DataTemplate(() => + { + var cell = new EntryCell(); + cell.SetBinding(EntryCell.TextProperty, "Value"); + return cell; + }), + ItemsSource = data + }; + + var layout = new VerticalStackLayout() + { + listView + }; + + await CreateHandlerAndAddToWindow(layout, async (handler) => + { + await Task.Yield(); + var entryCell = listView.TemplatedItems[0] as EntryCell; + + // Initial Value is correct + Assert.Equal(vm.Value, entryCell.Text); + + // Validate that the binding stays operational + for (int i = 0; i < 3; i++) + { + vm.ChangeValue(); + await Task.Yield(); + Assert.Equal(vm.Value, entryCell.Text); + } + }); + } + class EntryCellBindingCorrectlyUpdatesVM : INotifyPropertyChanged + { + public event PropertyChangedEventHandler PropertyChanged; + + public string Value { get; set; } + + public void ChangeValue() + { + Value = Guid.NewGuid().ToString(); + PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(Value))); + } + } + [Fact] public async Task ClearItemsListViewDoesntCrash() {