diff --git a/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml b/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml
index 46f5fc41..d99fddc7 100644
--- a/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml
+++ b/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml
@@ -1,21 +1,33 @@
-
+
+
+
+
+
+
+
+
+
- One
- Two
- Three
- Four
- Five
- Six
- Seven
- Eight
- Nine
- Ten
+ 0
+ 1
+ 2
+ 3
+ 4
+ 5
+ 6
+ 7
+ 8
+ 9
+ 10
diff --git a/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml.cs b/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml.cs
index 20318e11..6c6f2825 100644
--- a/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml.cs
+++ b/components/Extensions/samples/ListViewExtensionsAlternateColorSample.xaml.cs
@@ -14,4 +14,23 @@ public ListViewExtensionsAlternateColorSample()
{
this.InitializeComponent();
}
+
+ public static string NaiveHumanize(int num)
+ {
+ return num switch
+ {
+ 0 => "zero",
+ 1 => "one",
+ 2 => "two",
+ 3 => "three",
+ 4 => "four",
+ 5 => "five",
+ 6 => "six",
+ 7 => "seven",
+ 8 => "eight",
+ 9 => "nine",
+ 10 => "ten",
+ _ => num.ToString(),
+ };
+ }
}
diff --git a/components/Extensions/src/ListViewBase/ListViewExtensions.AlternateRows.cs b/components/Extensions/src/ListViewBase/ListViewExtensions.AlternateRows.cs
index 1e73c325..7fe9c745 100644
--- a/components/Extensions/src/ListViewBase/ListViewExtensions.AlternateRows.cs
+++ b/components/Extensions/src/ListViewBase/ListViewExtensions.AlternateRows.cs
@@ -10,202 +10,193 @@ namespace CommunityToolkit.WinUI;
///
public static partial class ListViewExtensions
{
- private static Dictionary, ListViewBase> _itemsForList = new Dictionary, ListViewBase>();
+ private static readonly Dictionary, ListViewBase> _trackedListViews = [];
///
/// Attached for binding a as an alternate background color to a
///
- public static readonly DependencyProperty AlternateColorProperty = DependencyProperty.RegisterAttached("AlternateColor", typeof(Brush), typeof(ListViewExtensions), new PropertyMetadata(null, OnAlternateColorPropertyChanged));
+ public static readonly DependencyProperty AlternateColorProperty =
+ DependencyProperty.RegisterAttached("AlternateColor", typeof(Brush), typeof(ListViewExtensions),
+ new PropertyMetadata(null, OnAlternateRowPropertyChanged));
+
+ ///
+ /// Attached for binding a as an alternate style to a
+ ///
+ public static readonly DependencyProperty AlternateStyleProperty =
+ DependencyProperty.RegisterAttached("AlternateStyle", typeof(Style), typeof(ListViewExtensions),
+ new PropertyMetadata(null, OnAlternateRowPropertyChanged));
///
/// Attached for binding a as an alternate template to a
///
- public static readonly DependencyProperty AlternateItemTemplateProperty = DependencyProperty.RegisterAttached("AlternateItemTemplate", typeof(DataTemplate), typeof(ListViewExtensions), new PropertyMetadata(null, OnAlternateItemTemplatePropertyChanged));
+ public static readonly DependencyProperty AlternateItemTemplateProperty =
+ DependencyProperty.RegisterAttached("AlternateItemTemplate", typeof(DataTemplate), typeof(ListViewExtensions),
+ new PropertyMetadata(null, OnAlternateRowPropertyChanged));
///
/// Gets the alternate associated with the specified
///
/// The to get the associated from
/// The associated with the
- public static Brush GetAlternateColor(ListViewBase obj)
- {
- return (Brush)obj.GetValue(AlternateColorProperty);
- }
+ public static Brush? GetAlternateColor(ListViewBase obj) => (Brush?)obj.GetValue(AlternateColorProperty);
///
/// Sets the alternate associated with the specified
///
/// The to associate the with
/// The for binding to the
- public static void SetAlternateColor(ListViewBase obj, Brush value)
- {
- obj.SetValue(AlternateColorProperty, value);
- }
+ public static void SetAlternateColor(ListViewBase obj, Brush? value) => obj.SetValue(AlternateColorProperty, value);
+
+ ///
+ /// Gets the alternate associated with the specified
+ ///
+ /// The to get the associated from
+ /// The associated with the
+ public static Style? GetAlternateStyle(ListViewBase obj) => (Style?)obj.GetValue(AlternateStyleProperty);
+
+ ///
+ /// Sets the alternate associated with the specified
+ ///
+ /// The to associate the with
+ /// The for binding to the
+ public static void SetAlternateStyle(ListViewBase obj, Style? value) => obj.SetValue(AlternateStyleProperty, value);
///
/// Gets the associated with the specified
///
/// The to get the associated from
/// The associated with the
- public static DataTemplate GetAlternateItemTemplate(ListViewBase obj)
- {
- return (DataTemplate)obj.GetValue(AlternateItemTemplateProperty);
- }
+ public static DataTemplate? GetAlternateItemTemplate(ListViewBase obj) => (DataTemplate?)obj.GetValue(AlternateItemTemplateProperty);
///
/// Sets the associated with the specified
///
/// The to associate the with
/// The for binding to the
- public static void SetAlternateItemTemplate(ListViewBase obj, DataTemplate value)
- {
- obj.SetValue(AlternateItemTemplateProperty, value);
- }
+ public static void SetAlternateItemTemplate(ListViewBase obj, DataTemplate? value) => obj.SetValue(AlternateItemTemplateProperty, value);
- private static void OnAlternateColorPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
+ private static void OnAlternateRowPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
{
- if (sender is ListViewBase listViewBase)
- {
- listViewBase.ContainerContentChanging -= ColorContainerContentChanging;
- listViewBase.Items.VectorChanged -= ColorItemsVectorChanged;
- listViewBase.Unloaded -= OnListViewBaseUnloaded;
-
- _itemsForList[listViewBase.Items] = listViewBase;
- if (AlternateColorProperty != null)
- {
- listViewBase.ContainerContentChanging += ColorContainerContentChanging;
- listViewBase.Items.VectorChanged += ColorItemsVectorChanged;
- listViewBase.Unloaded += OnListViewBaseUnloaded;
- }
- }
- }
+ if (sender is not ListViewBase listViewBase)
+ return;
- private static void ColorContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
- {
- var itemContainer = args.ItemContainer as Control;
- SetItemContainerBackground(sender, itemContainer, args.ItemIndex);
- }
+ // Cleanup existing subscriptions
+ listViewBase.ContainerContentChanging -= OnContainerContentChanging;
+ listViewBase.Items.VectorChanged -= OnItemsVectorChanged;
+ listViewBase.Unloaded -= OnListViewBaseUnloaded_AltRow;
- private static void OnAlternateItemTemplatePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
- {
- if (sender is ListViewBase listViewBase)
+ _trackedListViews[listViewBase.Items] = listViewBase;
+
+ // Resubscribe to events as necessary
+ var altColor = GetAlternateColor(listViewBase);
+ var altStyle = GetAlternateStyle(listViewBase);
+ var altTemplate = GetAlternateItemTemplate(listViewBase);
+
+ // If any of the properties are set, subscribe to the necessary events
+ if ((altColor ?? altStyle ?? (object?)altTemplate) is not null)
{
- listViewBase.ContainerContentChanging -= ItemTemplateContainerContentChanging;
- listViewBase.Unloaded -= OnListViewBaseUnloaded;
-
- if (AlternateItemTemplateProperty != null)
- {
- listViewBase.ContainerContentChanging += ItemTemplateContainerContentChanging;
- listViewBase.Unloaded += OnListViewBaseUnloaded;
- }
+ listViewBase.ContainerContentChanging += OnContainerContentChanging;
+ listViewBase.Items.VectorChanged += OnItemsVectorChanged;
+ listViewBase.Unloaded += OnListViewBaseUnloaded_AltRow;
}
+
+ // Update all items to apply the new property
+ UpdateItems(listViewBase);
}
- private static void ItemTemplateContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
+ private static void OnContainerContentChanging(ListViewBase sender, ContainerContentChangingEventArgs args) => UpdateItem(sender, args.ItemIndex);
+
+ private static void OnItemsVectorChanged(IObservableVector