Skip to content

Conversation

@Avid29
Copy link
Contributor

@Avid29 Avid29 commented Dec 19, 2025

Fixes #712

PR Type

What kind of change does this PR introduce?

What is the current behavior?

If a ListViewBase does not use a container, for whatever reason, the item's background will not be altered.

What is the new behavior?

If the ItemContainer is not, the item itself will be used if the type is a Control.

PR Checklist

Please check if your PR fulfills the following requirements:

Other information

Copy link

@zubinqayam zubinqayam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avid29:fix/alt-color6

@Avid29 Avid29 changed the title Fix AlternateColor when using ListViewBase without container [Extensions] AlternateColor without item containers Dec 19, 2025
Comment on lines 121 to 133
private static void OnListViewBaseUnloaded(object sender, RoutedEventArgs e)
{
if (sender is ListViewBase listViewBase)
{
_itemsForList.Remove(listViewBase.Items);

listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;
listViewBase.Unloaded -= OnListViewBaseUnloaded;
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you are already organizing code in this PR, this would be a good time to move OnListViewBaseUnloaded into its own file instead of keeping it in ListViewExtensions.AlternateRows.cs. That method handles unsubscribing from several features, so separating it would improve clarity and maintainability.

ListViewExtensions.cs

namespace CommunityToolkit.WinUI;

public static partial class ListViewExtensions
{
    private static void OnListViewBaseUnloaded(object sender, RoutedEventArgs e)
    {
        if (sender is not ListViewBase listViewBase)
            return;

        _itemsForList.Remove(listViewBase.Items);

        listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
        listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
        listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
        listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;
        listViewBase.Unloaded -= OnListViewBaseUnloaded;
    }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the reminder. I had noticed this, but hadn't yet written an alternative because I was torn between your solution, and splitting the method for the different attached property types.

ListViewExtensions.AlternateRows.cs

private static void OnListViewBaseUnloaded_AltRow(object sender, RoutedEventArgs e)
{
    if (sender is not ListViewBase listViewBase)
        return;

    // Untrack the list view
    _trackedListViews.Remove(listViewBase.Items);

    // Unsubscribe from events
    listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
    listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
    listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;
    listViewBase.Unloaded -= OnListViewBaseUnloaded_AltRow;
}

ListViewExtensions.StretchItemContainer.cs

private static void OnListViewBaseUnloaded_StretchDirection(object sender, RoutedEventArgs e)
{
    if (sender is not ListViewBase listViewBase)
        return;

    // Unsubscribe from events
    listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
    listViewBase.Unloaded -= OnListViewBaseUnloaded_StretchDirection;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One vote for your solution then. We can still keep each feature on its own file unless we have other shared logic. 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ListViewExtensions.AlternateColor doesn't work with ListView.Items with ListViewItem

3 participants