Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,19 +64,22 @@ public static void SetAlternateItemTemplate(ListViewBase obj, DataTemplate value

private static void OnAlternateColorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
if (sender is ListViewBase listViewBase)
{
listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;
listViewBase.Unloaded -= OnListViewBaseUnloaded;
if (sender is not ListViewBase listViewBase)
return;

_itemsForList[listViewBase.Items] = listViewBase;
if (AlternateColorProperty != null)
{
listViewBase.ContainerContentChanging += ColorContainerContentChanging;
listViewBase.Items.VectorChanged += ColorItemsVectorChanged;
listViewBase.Unloaded += OnListViewBaseUnloaded;
}
// Cleanup existing subscriptions
listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;
listViewBase.Unloaded -= OnListViewBaseUnloaded;

_itemsForList[listViewBase.Items] = listViewBase;

// Resubscribe to events as necessary
if (GetAlternateColor(listViewBase) is not null)
{
listViewBase.ContainerContentChanging += ColorContainerContentChanging;
listViewBase.Items.VectorChanged += ColorItemsVectorChanged;
listViewBase.Unloaded += OnListViewBaseUnloaded;
}
}

Expand All @@ -88,16 +91,18 @@ private static void ColorContainerContentChanging(ListViewBase sender, Container

private static void OnAlternateItemTemplatePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
if (sender is ListViewBase listViewBase)
{
listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
listViewBase.Unloaded -= OnListViewBaseUnloaded;
if (sender is not ListViewBase listViewBase)
return;

if (AlternateItemTemplateProperty != null)
{
listViewBase.ContainerContentChanging += ItemTemplateContainerContentChanging;
listViewBase.Unloaded += OnListViewBaseUnloaded;
}
// Cleanup existing subscriptions
listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
listViewBase.Unloaded -= OnListViewBaseUnloaded;

// Resubscribe to events as necessary
if (GetAlternateItemTemplate(listViewBase) != null)
{
listViewBase.ContainerContentChanging += ItemTemplateContainerContentChanging;
listViewBase.Unloaded += OnListViewBaseUnloaded;
}
}

Expand All @@ -113,36 +118,6 @@ private static void ItemTemplateContainerContentChanging(ListViewBase sender, Co
}
}

private static void OnItemContainerStretchDirectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
if (sender is ListViewBase listViewBase)
{
listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
listViewBase.Unloaded -= OnListViewBaseUnloaded;

if (ItemContainerStretchDirectionProperty != null)
{
listViewBase.ContainerContentChanging += ItemContainerStretchDirectionChanging;
listViewBase.Unloaded += OnListViewBaseUnloaded;
}
}
}

private static void ItemContainerStretchDirectionChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
var stretchDirection = GetItemContainerStretchDirection(sender);

if (stretchDirection == ItemContainerStretchDirection.Vertical || stretchDirection == ItemContainerStretchDirection.Both)
{
args.ItemContainer.VerticalContentAlignment = VerticalAlignment.Stretch;
}

if (stretchDirection == ItemContainerStretchDirection.Horizontal || stretchDirection == ItemContainerStretchDirection.Both)
{
args.ItemContainer.HorizontalContentAlignment = HorizontalAlignment.Stretch;
}
}

private static void OnListViewBaseUnloaded(object sender, RoutedEventArgs e)
{
if (sender is ListViewBase listViewBase)
Expand Down Expand Up @@ -171,15 +146,16 @@ private static void ColorItemsVectorChanged(IObservableVector<object> sender, IV
{
_itemsForList.TryGetValue(sender, out ListViewBase? listViewBase);
if (listViewBase == null)
{
return;
}

int index = (int)args.Index;
for (int i = index; i < sender.Count; i++)
{
// Get item container or element at index
var itemContainer = listViewBase.ContainerFromIndex(i) as Control;
if (itemContainer != null)
itemContainer ??= listViewBase.Items[i] as Control;

if (itemContainer is not null)
{
SetItemContainerBackground(listViewBase, itemContainer, i);
}
Expand All @@ -189,23 +165,13 @@ private static void ColorItemsVectorChanged(IObservableVector<object> sender, IV

private static void SetItemContainerBackground(ListViewBase sender, Control itemContainer, int itemIndex)
{
if (itemIndex % 2 == 0)
{
itemContainer.Background = GetAlternateColor(sender);
var rootBorder = itemContainer.FindDescendant<Border>();
if (rootBorder != null)
{
rootBorder.Background = GetAlternateColor(sender);
}
}
else
var brush = itemIndex % 2 == 0 ? GetAlternateColor(sender) : null;
var rootBorder = itemContainer.FindDescendant<Border>();

itemContainer.Background = brush;
if (rootBorder is not null)
{
itemContainer.Background = null;
var rootBorder = itemContainer.FindDescendant<Border>();
if (rootBorder != null)
{
rootBorder.Background = null;
}
rootBorder.Background = brush;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public static partial class ListViewExtensions
/// </summary>
/// <param name="obj">The <see cref="ListViewBase"/> to get the associated <see cref="ItemContainerStretchDirection"/> from</param>
/// <returns>The <see cref="ItemContainerStretchDirection"/> associated with the <see cref="ListViewBase"/></returns>
public static ItemContainerStretchDirection GetItemContainerStretchDirection(ListViewBase obj)
public static ItemContainerStretchDirection? GetItemContainerStretchDirection(ListViewBase obj)
{
return (ItemContainerStretchDirection)obj.GetValue(ItemContainerStretchDirectionProperty);
}
Expand All @@ -33,4 +33,37 @@ public static void SetItemContainerStretchDirection(ListViewBase obj, ItemContai
{
obj.SetValue(ItemContainerStretchDirectionProperty, value);
}

private static void OnItemContainerStretchDirectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
if (sender is not ListViewBase listViewBase)
return;

// Cleanup existing subscriptions
listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
listViewBase.Unloaded -= OnListViewBaseUnloaded;

// Resubscribe to events as necessary
if (GetItemContainerStretchDirection(listViewBase) is not null)
{
listViewBase.ContainerContentChanging += ItemContainerStretchDirectionChanging;
listViewBase.Unloaded += OnListViewBaseUnloaded;
}
}

private static void ItemContainerStretchDirectionChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
{
var stretchDirection = GetItemContainerStretchDirection(sender);

if (stretchDirection == ItemContainerStretchDirection.Vertical || stretchDirection == ItemContainerStretchDirection.Both)
{
args.ItemContainer.VerticalContentAlignment = VerticalAlignment.Stretch;
}

if (stretchDirection == ItemContainerStretchDirection.Horizontal || stretchDirection == ItemContainerStretchDirection.Both)
{
args.ItemContainer.HorizontalContentAlignment = HorizontalAlignment.Stretch;
}
}

}
Loading