Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
ObservableFieldItemDictionarySelector.cs
1using System;
2using System.Collections.ObjectModel;
3using System.Collections.Specialized;
4using System.Reflection;
5using Microsoft.Maui.Controls;
7
8
10{
14 public class TemplateMapping
15 {
19 public required string Key { get; set; }
20
24 public required DataTemplate Template { get; set; }
25 }
26 public class ObservableFieldItemDictionarySelector : DataTemplateSelector
27 {
28 // 1) Use ObservableCollection so we can hook CollectionChanged
29 public ObservableCollection<TemplateMapping> Templates { get; }
30 = [];
31
32 public DataTemplate? DefaultTemplate { get; set; }
33
34 // backing template map
35 private Dictionary<string, DataTemplate>? templateMap;
36
38 {
39 this.Templates.CollectionChanged += this.OnTemplatesChanged;
40 }
41
42 void OnTemplatesChanged(object? sender, NotifyCollectionChangedEventArgs e)
43 {
44 this.templateMap = null;
45 }
46
47 protected override DataTemplate? OnSelectTemplate(object item, BindableObject container)
48 {
49 // populate template map
50 this.templateMap ??= this.Templates
51 .Where(m => !string.IsNullOrEmpty(m.Key) && m.Template is not null)
52 .ToDictionary(m => m.Key!, m => m.Template!);
53
54 if (item is ObservableFieldItem Field
55 && !string.IsNullOrEmpty(Field.Key)
56 && this.templateMap.TryGetValue(Field.Key, out DataTemplate? Template))
57 {
58 return Template;
59 }
60
61 return this.DefaultTemplate;
62 }
63 }
64}
ViewModel for a single property field of a LegalIdentity.
A single mapping of an ObservableFieldItem.Key → DataTemplate.
required string Key
The string key to match, e.g. Constants.CustomXmppProperties.Neuro_Id.
required DataTemplate Template
The template to use when an item’s Key equals Key.