Skip to content

Commit 51b6906

Browse files
Avid29Arlodotexe
authored andcommitted
Fixed unneccesary event subscriptions in ListViewExtensions
1 parent f76e6f8 commit 51b6906

2 files changed

Lines changed: 60 additions & 52 deletions

File tree

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

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

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

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static partial class ListViewExtensions
1919
/// </summary>
2020
/// <param name="obj">The <see cref="ListViewBase"/> to get the associated <see cref="ItemContainerStretchDirection"/> from</param>
2121
/// <returns>The <see cref="ItemContainerStretchDirection"/> associated with the <see cref="ListViewBase"/></returns>
22-
public static ItemContainerStretchDirection GetItemContainerStretchDirection(ListViewBase obj)
22+
public static ItemContainerStretchDirection? GetItemContainerStretchDirection(ListViewBase obj)
2323
{
2424
return (ItemContainerStretchDirection)obj.GetValue(ItemContainerStretchDirectionProperty);
2525
}
@@ -33,4 +33,37 @@ public static void SetItemContainerStretchDirection(ListViewBase obj, ItemContai
3333
{
3434
obj.SetValue(ItemContainerStretchDirectionProperty, value);
3535
}
36+
37+
private static void OnItemContainerStretchDirectionPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
38+
{
39+
if (sender is not ListViewBase listViewBase)
40+
return;
41+
42+
// Cleanup existing subscriptions
43+
listViewBase.ContainerContentChanging -= ItemContainerStretchDirectionChanging;
44+
listViewBase.Unloaded -= OnListViewBaseUnloaded;
45+
46+
// Resubscribe to events as necessary
47+
if (GetItemContainerStretchDirection(listViewBase) is not null)
48+
{
49+
listViewBase.ContainerContentChanging += ItemContainerStretchDirectionChanging;
50+
listViewBase.Unloaded += OnListViewBaseUnloaded;
51+
}
52+
}
53+
54+
private static void ItemContainerStretchDirectionChanging(ListViewBase sender, ContainerContentChangingEventArgs args)
55+
{
56+
var stretchDirection = GetItemContainerStretchDirection(sender);
57+
58+
if (stretchDirection == ItemContainerStretchDirection.Vertical || stretchDirection == ItemContainerStretchDirection.Both)
59+
{
60+
args.ItemContainer.VerticalContentAlignment = VerticalAlignment.Stretch;
61+
}
62+
63+
if (stretchDirection == ItemContainerStretchDirection.Horizontal || stretchDirection == ItemContainerStretchDirection.Both)
64+
{
65+
args.ItemContainer.HorizontalContentAlignment = HorizontalAlignment.Stretch;
66+
}
67+
}
68+
3669
}

0 commit comments

Comments
 (0)