-
Notifications
You must be signed in to change notification settings - Fork 807
Expand file tree
/
Copy pathCollectionPropertyBooleanOrConverter.cs
More file actions
63 lines (50 loc) · 2.06 KB
/
CollectionPropertyBooleanOrConverter.cs
File metadata and controls
63 lines (50 loc) · 2.06 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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Globalization;
using System.Reflection;
using System.Windows.Data;
using NETworkManager.Interfaces.ViewModels;
namespace NETworkManager.Converters
{
/// <summary>
/// A generic converter that checks a property of items in a collection.
/// If ANY item's property is considered "present" (not null, not empty), it returns Visible.
/// </summary>
/// <typeparam name="T">The type of item in the collection.</typeparam>
public class CollectionPropertyBooleanOrConverter<T> : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
// 1. Validate inputs
if (value is not IEnumerable<T> collection)
return false;
if (parameter is not string propertyName || string.IsNullOrEmpty(propertyName))
return false;
// 2. Get PropertyInfo via Reflection or cache.
if (!Cache.TryGetValue(propertyName, out var propertyInfo))
{
propertyInfo = typeof(T).GetProperty(propertyName);
Cache.TryAdd(propertyName, propertyInfo);
}
if (propertyInfo == null)
return false;
// 3. Iterate collection and check property
foreach (var item in collection)
{
if (item == null) continue;
var propValue = propertyInfo.GetValue(item);
if (propValue is true)
return true;
}
return false;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
private ConcurrentDictionary<string, PropertyInfo> Cache { get; } = new();
}
// Concrete implementation for XAML usage
public class FirewallRuleViewModelBooleanOrConverter : CollectionPropertyBooleanOrConverter<IFirewallRuleViewModel>;
}