@@ -64,19 +64,22 @@ public static void SetAlternateItemTemplate(ListViewBase obj, DataTemplate value
6464
6565 private static void OnAlternateColorPropertyChanged ( DependencyObject sender , DependencyPropertyChangedEventArgs args )
6666 {
67- if ( sender is ListViewBase listViewBase )
68- {
69- listViewBase . ContainerContentChanging -= ColorContainerContentChanging ;
70- listViewBase . Items . VectorChanged -= ColorItemsVectorChanged ;
71- listViewBase . Unloaded -= OnListViewBaseUnloaded ;
67+ if ( sender is not ListViewBase listViewBase )
68+ return ;
7269
73- _itemsForList [ listViewBase . Items ] = listViewBase ;
74- if ( AlternateColorProperty != null )
75- {
76- listViewBase . ContainerContentChanging += ColorContainerContentChanging ;
77- listViewBase . Items . VectorChanged += ColorItemsVectorChanged ;
78- listViewBase . Unloaded += OnListViewBaseUnloaded ;
79- }
70+ // Cleanup existing subscriptions
71+ listViewBase . ContainerContentChanging -= ColorContainerContentChanging ;
72+ listViewBase . Items . VectorChanged -= ColorItemsVectorChanged ;
73+ listViewBase . Unloaded -= OnListViewBaseUnloaded ;
74+
75+ _itemsForList [ listViewBase . Items ] = listViewBase ;
76+
77+ // Resubscribe to events as necessary
78+ if ( GetAlternateColor ( listViewBase ) is not null )
79+ {
80+ listViewBase . ContainerContentChanging += ColorContainerContentChanging ;
81+ listViewBase . Items . VectorChanged += ColorItemsVectorChanged ;
82+ listViewBase . Unloaded += OnListViewBaseUnloaded ;
8083 }
8184 }
8285
@@ -88,16 +91,18 @@ private static void ColorContainerContentChanging(ListViewBase sender, Container
8891
8992 private static void OnAlternateItemTemplatePropertyChanged ( DependencyObject sender , DependencyPropertyChangedEventArgs args )
9093 {
91- if ( sender is ListViewBase listViewBase )
92- {
93- listViewBase . ContainerContentChanging -= ItemTemplateContainerContentChanging ;
94- listViewBase . Unloaded -= OnListViewBaseUnloaded ;
94+ if ( sender is not ListViewBase listViewBase )
95+ return ;
9596
96- if ( AlternateItemTemplateProperty != null )
97- {
98- listViewBase . ContainerContentChanging += ItemTemplateContainerContentChanging ;
99- listViewBase . Unloaded += OnListViewBaseUnloaded ;
100- }
97+ // Cleanup existing subscriptions
98+ listViewBase . ContainerContentChanging -= ItemTemplateContainerContentChanging ;
99+ listViewBase . Unloaded -= OnListViewBaseUnloaded ;
100+
101+ // Resubscribe to events as necessary
102+ if ( GetAlternateItemTemplate ( listViewBase ) != null )
103+ {
104+ listViewBase . ContainerContentChanging += ItemTemplateContainerContentChanging ;
105+ listViewBase . Unloaded += OnListViewBaseUnloaded ;
101106 }
102107 }
103108
@@ -113,36 +118,6 @@ private static void ItemTemplateContainerContentChanging(ListViewBase sender, Co
113118 }
114119 }
115120
116- private static void OnItemContainerStretchDirectionPropertyChanged ( DependencyObject sender , DependencyPropertyChangedEventArgs args )
117- {
118- if ( sender is ListViewBase listViewBase )
119- {
120- listViewBase . ContainerContentChanging -= ItemContainerStretchDirectionChanging ;
121- listViewBase . Unloaded -= OnListViewBaseUnloaded ;
122-
123- if ( ItemContainerStretchDirectionProperty != null )
124- {
125- listViewBase . ContainerContentChanging += ItemContainerStretchDirectionChanging ;
126- listViewBase . Unloaded += OnListViewBaseUnloaded ;
127- }
128- }
129- }
130-
131- private static void ItemContainerStretchDirectionChanging ( ListViewBase sender , ContainerContentChangingEventArgs args )
132- {
133- var stretchDirection = GetItemContainerStretchDirection ( sender ) ;
134-
135- if ( stretchDirection == ItemContainerStretchDirection . Vertical || stretchDirection == ItemContainerStretchDirection . Both )
136- {
137- args . ItemContainer . VerticalContentAlignment = VerticalAlignment . Stretch ;
138- }
139-
140- if ( stretchDirection == ItemContainerStretchDirection . Horizontal || stretchDirection == ItemContainerStretchDirection . Both )
141- {
142- args . ItemContainer . HorizontalContentAlignment = HorizontalAlignment . Stretch ;
143- }
144- }
145-
146121 private static void OnListViewBaseUnloaded ( object sender , RoutedEventArgs e )
147122 {
148123 if ( sender is ListViewBase listViewBase )
0 commit comments