This repository was archived by the owner on Feb 25, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathIsNullOrEmptyStateTrigger.cs
More file actions
152 lines (131 loc) · 5.71 KB
/
IsNullOrEmptyStateTrigger.cs
File metadata and controls
152 lines (131 loc) · 5.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections;
using System.Collections.Specialized;
using Microsoft.Toolkit.Uwp.Helpers;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
namespace Microsoft.Toolkit.Uwp.UI.Triggers
{
/// <summary>
/// Enables a state if an Object is <c>null</c> or a String/IEnumerable is empty
/// </summary>
public class IsNullOrEmptyStateTrigger : StateTriggerBase
{
/// <summary>
/// Gets or sets the value used to check for <c>null</c> or empty.
/// </summary>
public object Value
{
get { return GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}
/// <summary>
/// Identifies the <see cref="Value"/> DependencyProperty
/// </summary>
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register(nameof(Value), typeof(object), typeof(IsNullOrEmptyStateTrigger), new PropertyMetadata(null, OnValuePropertyChanged));
/// <summary>
/// Gets or sets a value indicating whether a trigger is active.
/// </summary>
public bool IsActive
{
get => (bool)GetValue(IsActiveProperty);
set => SetValue(IsActiveProperty, value);
}
/// <summary>
/// Identifies the <see cref="IsActive"/> DependencyProperty
/// Allows user to enable the trigger from XAML
/// </summary>
public static readonly DependencyProperty IsActiveProperty =
DependencyProperty.Register(nameof(IsActive), typeof(bool), typeof(IsNullOrEmptyStateTrigger), new PropertyMetadata(false, OnIsActiveChanged));
private static void OnIsActiveChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue != null && e.NewValue is bool)
{
var obj = (IsNullOrEmptyStateTrigger)d;
obj.SetActive((bool)e.NewValue);
}
}
private static void OnValuePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var obj = (IsNullOrEmptyStateTrigger)d;
var val = e.NewValue;
obj.SetActive(obj.IsActive = IsNullOrEmpty(val));
if (val == null)
{
return;
}
// Try to listen for various notification events
// Starting with INotifyCollectionChanged
var valNotifyCollection = val as INotifyCollectionChanged;
if (valNotifyCollection != null)
{
var weakEvent = new WeakEventListener<INotifyCollectionChanged, object, NotifyCollectionChangedEventArgs>(valNotifyCollection)
{
OnEventAction = (instance, source, args) => obj.SetActive(IsNullOrEmpty(instance)),
OnDetachAction = (weakEventListener) => valNotifyCollection.CollectionChanged -= weakEventListener.OnEvent
};
valNotifyCollection.CollectionChanged += weakEvent.OnEvent;
return;
}
// Not INotifyCollectionChanged, try IObservableVector
var valObservableVector = val as IObservableVector<object>;
if (valObservableVector != null)
{
var weakEvent = new WeakEventListener<IObservableVector<object>, object, IVectorChangedEventArgs>(valObservableVector)
{
OnEventAction = (instance, source, args) => obj.SetActive(IsNullOrEmpty(instance)),
OnDetachAction = (weakEventListener) => valObservableVector.VectorChanged -= weakEventListener.OnEvent
};
valObservableVector.VectorChanged += weakEvent.OnEvent;
return;
}
// Not INotifyCollectionChanged, try IObservableMap
var valObservableMap = val as IObservableMap<object, object>;
if (valObservableMap != null)
{
var weakEvent = new WeakEventListener<IObservableMap<object, object>, object, IMapChangedEventArgs<object>>(valObservableMap)
{
OnEventAction = (instance, source, args) => obj.SetActive(IsNullOrEmpty(instance)),
OnDetachAction = (weakEventListener) => valObservableMap.MapChanged -= weakEventListener.OnEvent
};
valObservableMap.MapChanged += weakEvent.OnEvent;
}
}
private static bool IsNullOrEmpty(object val)
{
if (val == null)
{
return true;
}
// Object is not null, check for an empty string
var valString = val as string;
if (valString != null)
{
return valString.Length == 0;
}
// Object is not a string, check for an empty ICollection (faster)
var valCollection = val as ICollection;
if (valCollection != null)
{
return valCollection.Count == 0;
}
// Object is not an ICollection, check for an empty IEnumerable
var valEnumerable = val as IEnumerable;
if (valEnumerable != null)
{
foreach (var item in valEnumerable)
{
// Found an item, not empty
return false;
}
return true;
}
// Not null and not a known type to test for emptiness
return false;
}
}
}