Skip to content

Commit 2954509

Browse files
committed
Added item fallback if container is null in ListViewExtensions alternate rows
1 parent 0bed1b7 commit 2954509

1 file changed

Lines changed: 9 additions & 21 deletions

File tree

components/Extensions/src/ListViewBase/ListViewExtensions.AlternateRows.cs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,16 @@ private static void ColorItemsVectorChanged(IObservableVector<object> sender, IV
171171
{
172172
_itemsForList.TryGetValue(sender, out ListViewBase? listViewBase);
173173
if (listViewBase == null)
174-
{
175174
return;
176-
}
177175

178176
int index = (int)args.Index;
179177
for (int i = index; i < sender.Count; i++)
180178
{
179+
// Get item container or element at index
181180
var itemContainer = listViewBase.ContainerFromIndex(i) as Control;
182-
if (itemContainer != null)
181+
itemContainer ??= listViewBase.Items[i] as Control;
182+
183+
if (itemContainer is not null)
183184
{
184185
SetItemContainerBackground(listViewBase, itemContainer, i);
185186
}
@@ -189,23 +190,10 @@ private static void ColorItemsVectorChanged(IObservableVector<object> sender, IV
189190

190191
private static void SetItemContainerBackground(ListViewBase sender, Control itemContainer, int itemIndex)
191192
{
192-
if (itemIndex % 2 == 0)
193-
{
194-
itemContainer.Background = GetAlternateColor(sender);
195-
var rootBorder = itemContainer.FindDescendant<Border>();
196-
if (rootBorder != null)
197-
{
198-
rootBorder.Background = GetAlternateColor(sender);
199-
}
200-
}
201-
else
202-
{
203-
itemContainer.Background = null;
204-
var rootBorder = itemContainer.FindDescendant<Border>();
205-
if (rootBorder != null)
206-
{
207-
rootBorder.Background = null;
208-
}
209-
}
193+
var brush = itemIndex % 2 == 0 ? GetAlternateColor(sender) : null;
194+
var rootBorder = itemContainer.FindDescendant<Border>();
195+
196+
itemContainer.Background = brush;
197+
rootBorder?.Background = brush;
210198
}
211199
}

0 commit comments

Comments
 (0)