Skip to content

Feature request: pick up on INotifyCollectionChanged changes to ItemsSource #104

Description

@mfeingol

That way the user can use an ObservableCollection and doesn't have to replace the existing collection. Much more aligned with how other ItemsSource properties work out there.

E.g. like this:

    /// <summary>
    /// Maps the <see cref="IAutoSuggestBox.ItemsSource"/> property to the native AutoSuggestBox control.
    /// </summary>
    /// <param name="handler">View handler</param>
    /// <param name="autoSuggestBox">IAutoSuggestBox instance</param>
    public static void MapItemsSource(AutoSuggestBoxHandler handler, IAutoSuggestBox autoSuggestBox)
    {
#if WINDOWS
    handler.PlatformView.ItemsSource = autoSuggestBox?.ItemsSource;
#elif __ANDROID__ || __IOS__
        // Unhook previous subscription
        if (handler.ItemsSourceSubscription is INotifyCollectionChanged oldNcc)
            oldNcc.CollectionChanged -= handler.OnItemsSourceCollectionChanged;

        // Apply initial items
        handler.PlatformView.SetItems(
            autoSuggestBox?.ItemsSource?.OfType<object>(),
            o => FormatType(o, autoSuggestBox?.DisplayMemberPath),
            o => FormatType(o, autoSuggestBox?.TextMemberPath)
        );

        // Hook new subscription
        if (autoSuggestBox?.ItemsSource is INotifyCollectionChanged ncc)
        {
            handler.ItemsSourceSubscription = ncc;
            ncc.CollectionChanged += handler.OnItemsSourceCollectionChanged;
        }
        else
        {
            handler.ItemsSourceSubscription = null;
        }
#endif
    }

#if __ANDROID__ || __IOS__
    INotifyCollectionChanged? ItemsSourceSubscription { get; set; }

    void OnItemsSourceCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        if (VirtualView is not IAutoSuggestBox autoSuggestBox)
            return;

        PlatformView.SetItems(
            autoSuggestBox.ItemsSource?.OfType<object>(),
            o => FormatType(o, autoSuggestBox.DisplayMemberPath),
            o => FormatType(o, autoSuggestBox.TextMemberPath)
        );
    }
#endif

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions