Make FadeAnimateItemsBehavior generic for all ItemsControls

This commit is contained in:
Yoshi Askharoun
2024-05-13 22:55:44 -05:00
parent d0c8c99e05
commit 8769c62d0a
@@ -4,75 +4,75 @@ using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;
using System.Windows.Threading;
namespace ZuneModdingHelper.Behaviors
namespace ZuneModdingHelper.Behaviors;
// https://stackoverflow.com/a/5910833
public class FadeAnimateItemsBehavior : Behavior<ItemsControl>
{
// https://stackoverflow.com/a/5910833
public class FadeAnimateItemsBehavior : Behavior<ListBox>
public DoubleAnimation Animation { get; set; }
public TimeSpan Tick { get; set; }
protected override void OnAttached()
{
public DoubleAnimation Animation { get; set; }
public TimeSpan Tick { get; set; }
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
}
protected override void OnAttached()
void AssociatedObject_Loaded(object sender, RoutedEventArgs e)
{
IEnumerable<UIElement> items;
if (AssociatedObject.ItemsSource == null)
{
base.OnAttached();
AssociatedObject.Loaded += AssociatedObject_Loaded;
items = AssociatedObject.Items.Cast<UIElement>();
}
void AssociatedObject_Loaded(object sender, System.Windows.RoutedEventArgs e)
else
{
IEnumerable<ListBoxItem> items;
if (AssociatedObject.ItemsSource == null)
var itemsSource = AssociatedObject.ItemsSource;
if (itemsSource is INotifyCollectionChanged)
{
items = AssociatedObject.Items.Cast<ListBoxItem>();
}
else
{
var itemsSource = AssociatedObject.ItemsSource;
if (itemsSource is INotifyCollectionChanged)
var collection = itemsSource as INotifyCollectionChanged;
collection.CollectionChanged += (s, cce) =>
{
var collection = itemsSource as INotifyCollectionChanged;
collection.CollectionChanged += (s, cce) =>
if (cce.Action == NotifyCollectionChangedAction.Add)
{
if (cce.Action == NotifyCollectionChangedAction.Add)
{
var itemContainer = AssociatedObject.ItemContainerGenerator.ContainerFromItem(cce.NewItems[0]) as ListBoxItem;
itemContainer.BeginAnimation(ListBoxItem.OpacityProperty, Animation);
}
};
}
ListBoxItem[] itemsSub = new ListBoxItem[AssociatedObject.Items.Count];
for (int i = 0; i < itemsSub.Length; i++)
{
itemsSub[i] = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(i) as ListBoxItem;
}
items = itemsSub;
}
foreach (var item in items.PruneNull())
{
item.Opacity = 0;
}
var enumerator = items.GetEnumerator();
if (enumerator.MoveNext())
{
DispatcherTimer timer = new() { Interval = Tick };
timer.Tick += (s, timerE) =>
{
var item = enumerator.Current;
if (item is null) return;
item.BeginAnimation(ListBoxItem.OpacityProperty, Animation);
if (!enumerator.MoveNext())
{
timer.Stop();
var itemContainer = AssociatedObject.ItemContainerGenerator.ContainerFromItem(cce.NewItems[0]) as UIElement;
itemContainer.BeginAnimation(UIElement.OpacityProperty, Animation);
}
};
timer.Start();
}
UIElement[] itemsSub = new UIElement[AssociatedObject.Items.Count];
for (int i = 0; i < itemsSub.Length; i++)
{
itemsSub[i] = AssociatedObject.ItemContainerGenerator.ContainerFromIndex(i) as UIElement;
}
items = itemsSub;
}
foreach (var item in items.PruneNull())
{
item.Opacity = 0;
}
var enumerator = items.GetEnumerator();
if (enumerator.MoveNext())
{
DispatcherTimer timer = new() { Interval = Tick };
timer.Tick += (s, timerE) =>
{
var item = enumerator.Current;
if (item is null) return;
item.BeginAnimation(UIElement.OpacityProperty, Animation);
if (!enumerator.MoveNext())
{
timer.Stop();
}
};
timer.Start();
}
}
}