Skip to content

Commit

Permalink
[Windows] Notify changes in Picker ItemsSource (#9584) Fixes #9239 Fixes
Browse files Browse the repository at this point in the history
 #9496

* Notify changes in Picker ItemsSource (without using ObservableCollection)

* Added device test
  • Loading branch information
jsuarezruiz authored Aug 23, 2022
1 parent 6bc7275 commit 7689601
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Controls/src/Core/HandlerImpl/Picker/Picker.Impl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ string GetItem(int index)
if (index < Items?.Count)
{
var item = Items[index];
return item == null ? string.Empty : item;
return item ?? string.Empty;
}

return string.Empty;
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/Picker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ void ResetItems()
((LockableObservableListWrapper)Items).InternalClear();
foreach (object item in ItemsSource)
((LockableObservableListWrapper)Items).InternalAdd(GetDisplayMember(item));
Handler?.UpdateValue(nameof(IPicker.Items));
UpdateSelectedItem(SelectedIndex);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,49 @@
using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.Maui.DeviceTests.Stubs;
using Microsoft.UI.Xaml.Controls;
using Xunit;

namespace Microsoft.Maui.DeviceTests
{
public partial class PickerHandlerTests
{
[Fact(DisplayName = "ItemsSource Updates Correctly")]
public async Task ItemsSourceUpdatesCorrectly()
{
int selectedIndex = 0;

var oldItems = new List<string>
{
"Old Item 1",
"Old Item 2",
"Old Item 3"
};

var newItems = new List<string>
{
"New Item 1",
"New Item 2",
"New Item 3"
};

var picker = new PickerStub()
{
Title = "Select an Item",
ItemsSource = oldItems,
SelectedIndex = 1
};

picker.ItemsSource = newItems;
picker.SelectedIndex = selectedIndex;

string expected = picker.ItemsSource[selectedIndex].ToString();
var actual = await GetValueAsync(picker, handler => GetNativeText(handler));

Assert.Equal(expected, actual);
}


ComboBox GetNativePicker(PickerHandler pickerHandler) =>
pickerHandler.PlatformView;

Expand All @@ -12,5 +52,12 @@ UI.Xaml.HorizontalAlignment GetNativeHorizontalTextAlignment(PickerHandler picke

UI.Xaml.VerticalAlignment GetNativeVerticalTextAlignment(PickerHandler pickerHandler) =>
GetNativePicker(pickerHandler).VerticalAlignment;

string GetNativeText(PickerHandler pickerHandler)
{
var comboBox = GetNativePicker(pickerHandler);

return comboBox.Items[comboBox.SelectedIndex].ToString();
}
}
}
22 changes: 20 additions & 2 deletions src/Core/tests/DeviceTests/Stubs/PickerStub.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections;
using System;
using System.Collections;
using System.Collections.Generic;
using Microsoft.Maui.Graphics;

Expand All @@ -12,7 +13,24 @@ public partial class PickerStub : StubBase, IPicker

public IList<string> Items { get; set; } = new List<string>();

public IList ItemsSource { get; set; }
IList _itemsSource;

public IList ItemsSource
{
get => _itemsSource;
set => SetProperty(ref _itemsSource, value, onChanged: OnItemsSourceChanged);
}

void OnItemsSourceChanged(IList oldValue, IList newValue)
{
if (ItemsSource == null)
return;

Items.Clear();

foreach (object item in ItemsSource)
Items.Add(item.ToString());
}

public int SelectedIndex { get; set; } = -1;

Expand Down

0 comments on commit 7689601

Please sign in to comment.