Search code examples
c#listviewuwpwin-universal-app

UWP ListView Update Row Background After Drag and Drop


I am currently doing this to make my ListView have an alternating row background.

    private void SongsListView_ContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
    {
        if (AlternatingRowColor)
            args.ItemContainer.Background = args.ItemIndex % 2 == 0 ? Helper.WhiteSmokeBrush : Helper.WhiteBrush;
    }

Additionally, I also allow my ListView to be able to drag and drop items. However, the row color doesn't update its color after dropping even though I do this:

    private void SongsListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
    {
        sender.UpdateLayout(); // Refresh Row Color
    }

meaning that my ListView does not have an alternating background after drag and drop.

How should I update its background?


Solution

  • An easier solution is this:

        private void SongsListView_DragItemsCompleted(ListViewBase sender, DragItemsCompletedEventArgs args)
        {
            for(int i = 0; i < sender.Items.Count; i++)
            {
                var container = sender.ContainerFromIndex(i) as ListViewItem;
                container.Background = i % 2 == 0 ? Helper.WhiteSmokeBrush : Helper.WhiteBrush;
            }
        }