|
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using Windows.Foundation.Collections; |
| 4 | +using Windows.UI.Xaml.Markup; |
| 5 | +using Microsoft.UI.Xaml; |
| 6 | +using Microsoft.UI.Xaml.Data; |
| 7 | +using Microsoft.UI.Xaml.Media; |
| 8 | + |
| 9 | +namespace Coder.Desktop.App.Converters; |
| 10 | + |
| 11 | +// This file uses manual DependencyProperty properties rather than |
| 12 | +// DependencyPropertyGenerator since it doesn't seem to work properly with |
| 13 | +// generics. |
| 14 | + |
| 15 | +/// <summary> |
| 16 | +/// An item in a DependencyObjectSelector. Each item has a key and a value. |
| 17 | +/// The default item in a DependencyObjectSelector will be the only item |
| 18 | +/// with a null key. |
| 19 | +/// </summary> |
| 20 | +/// <typeparam name="TK">Key type</typeparam> |
| 21 | +/// <typeparam name="TV">Value type</typeparam> |
| 22 | +public class DependencyObjectSelectorItem<TK, TV> : DependencyObject |
| 23 | + where TK : IEquatable<TK> |
| 24 | +{ |
| 25 | + public static readonly DependencyProperty KeyProperty = |
| 26 | + DependencyProperty.Register(nameof(Key), |
| 27 | + typeof(TK?), |
| 28 | + typeof(DependencyObjectSelectorItem<TK, TV>), |
| 29 | + new PropertyMetadata(null)); |
| 30 | + |
| 31 | + public static readonly DependencyProperty ValueProperty = |
| 32 | + DependencyProperty.Register(nameof(Value), |
| 33 | + typeof(TV?), |
| 34 | + typeof(DependencyObjectSelectorItem<TK, TV>), |
| 35 | + new PropertyMetadata(null)); |
| 36 | + |
| 37 | + public TK? Key |
| 38 | + { |
| 39 | + get => (TK?)GetValue(KeyProperty); |
| 40 | + set => SetValue(KeyProperty, value); |
| 41 | + } |
| 42 | + |
| 43 | + public TV? Value |
| 44 | + { |
| 45 | + get => (TV?)GetValue(ValueProperty); |
| 46 | + set => SetValue(ValueProperty, value); |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +/// <summary> |
| 51 | +/// Allows selecting between multiple value references based on a selected |
| 52 | +/// key. This allows for dynamic mapping of model values to other objects. |
| 53 | +/// The main use case is for selecting between other bound values, which |
| 54 | +/// you cannot do with a simple ValueConverter. |
| 55 | +/// </summary> |
| 56 | +/// <typeparam name="TK">Key type</typeparam> |
| 57 | +/// <typeparam name="TV">Value type</typeparam> |
| 58 | +[ContentProperty(Name = nameof(References))] |
| 59 | +public class DependencyObjectSelector<TK, TV> : DependencyObject |
| 60 | + where TK : IEquatable<TK> |
| 61 | +{ |
| 62 | + public static readonly DependencyProperty ReferencesProperty = |
| 63 | + DependencyProperty.Register(nameof(References), |
| 64 | + typeof(DependencyObjectCollection), |
| 65 | + typeof(DependencyObjectSelector<TK, TV>), |
| 66 | + new PropertyMetadata(null, ReferencesPropertyChanged)); |
| 67 | + |
| 68 | + public static readonly DependencyProperty SelectedKeyProperty = |
| 69 | + DependencyProperty.Register(nameof(SelectedKey), |
| 70 | + typeof(TK?), |
| 71 | + typeof(DependencyObjectSelector<TK, TV>), |
| 72 | + new PropertyMetadata(null, SelectedKeyPropertyChanged)); |
| 73 | + |
| 74 | + public static readonly DependencyProperty SelectedObjectProperty = |
| 75 | + DependencyProperty.Register(nameof(SelectedObject), |
| 76 | + typeof(TV?), |
| 77 | + typeof(DependencyObjectSelector<TK, TV>), |
| 78 | + new PropertyMetadata(null)); |
| 79 | + |
| 80 | + public DependencyObjectCollection? References |
| 81 | + { |
| 82 | + get => (DependencyObjectCollection?)GetValue(ReferencesProperty); |
| 83 | + set |
| 84 | + { |
| 85 | + // Ensure unique keys and that the values are DependencyObjectSelectorItem<K, V>. |
| 86 | + if (value != null) |
| 87 | + { |
| 88 | + var items = value.OfType<DependencyObjectSelectorItem<TK, TV>>().ToArray(); |
| 89 | + var keys = items.Select(i => i.Key).Distinct().ToArray(); |
| 90 | + if (keys.Length != value.Count) |
| 91 | + throw new ArgumentException("ObservableCollection Keys must be unique."); |
| 92 | + } |
| 93 | + |
| 94 | + SetValue(ReferencesProperty, value); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// <summary> |
| 99 | + /// The key of the selected item. This should be bound to a property on |
| 100 | + /// the model. |
| 101 | + /// </summary> |
| 102 | + public TK? SelectedKey |
| 103 | + { |
| 104 | + get => (TK?)GetValue(SelectedKeyProperty); |
| 105 | + set => SetValue(SelectedKeyProperty, value); |
| 106 | + } |
| 107 | + |
| 108 | + /// <summary> |
| 109 | + /// The selected object. This can be read from to get the matching |
| 110 | + /// object for the selected key. If the selected key doesn't match any |
| 111 | + /// object, this will be the value of the null key. If there is no null |
| 112 | + /// key, this will be null. |
| 113 | + /// </summary> |
| 114 | + public TV? SelectedObject |
| 115 | + { |
| 116 | + get => (TV?)GetValue(SelectedObjectProperty); |
| 117 | + set => SetValue(SelectedObjectProperty, value); |
| 118 | + } |
| 119 | + |
| 120 | + public DependencyObjectSelector() |
| 121 | + { |
| 122 | + References = []; |
| 123 | + } |
| 124 | + |
| 125 | + private void UpdateSelectedObject() |
| 126 | + { |
| 127 | + if (References != null) |
| 128 | + { |
| 129 | + // Look for a matching item a matching key, or fallback to the null |
| 130 | + // key. |
| 131 | + var references = References.OfType<DependencyObjectSelectorItem<TK, TV>>().ToArray(); |
| 132 | + var item = references |
| 133 | + .FirstOrDefault(i => |
| 134 | + (i.Key == null && SelectedKey == null) || |
| 135 | + (i.Key != null && SelectedKey != null && i.Key!.Equals(SelectedKey!))) |
| 136 | + ?? references.FirstOrDefault(i => i.Key == null); |
| 137 | + if (item is not null) |
| 138 | + { |
| 139 | + // Bind the SelectedObject property to the reference's Value. |
| 140 | + // If the underlying Value changes, it will propagate to the |
| 141 | + // SelectedObject. |
| 142 | + BindingOperations.SetBinding |
| 143 | + ( |
| 144 | + this, |
| 145 | + SelectedObjectProperty, |
| 146 | + new Binding |
| 147 | + { |
| 148 | + Source = item, |
| 149 | + Path = new PropertyPath(nameof(DependencyObjectSelectorItem<TK, TV>.Value)), |
| 150 | + } |
| 151 | + ); |
| 152 | + return; |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + ClearValue(SelectedObjectProperty); |
| 157 | + } |
| 158 | + |
| 159 | + // Called when the References property is replaced. |
| 160 | + private static void ReferencesPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) |
| 161 | + { |
| 162 | + var self = obj as DependencyObjectSelector<TK, TV>; |
| 163 | + if (self == null) return; |
| 164 | + var oldValue = args.OldValue as DependencyObjectCollection; |
| 165 | + if (oldValue != null) |
| 166 | + oldValue.VectorChanged -= self.OnVectorChangedReferences; |
| 167 | + var newValue = args.NewValue as DependencyObjectCollection; |
| 168 | + if (newValue != null) |
| 169 | + newValue.VectorChanged += self.OnVectorChangedReferences; |
| 170 | + } |
| 171 | + |
| 172 | + // Called when the References collection changes without being replaced. |
| 173 | + private void OnVectorChangedReferences(IObservableVector<DependencyObject> sender, IVectorChangedEventArgs args) |
| 174 | + { |
| 175 | + UpdateSelectedObject(); |
| 176 | + } |
| 177 | + |
| 178 | + // Called when SelectedKey changes. |
| 179 | + private static void SelectedKeyPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) |
| 180 | + { |
| 181 | + var self = obj as DependencyObjectSelector<TK, TV>; |
| 182 | + self?.UpdateSelectedObject(); |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +public sealed class StringToBrushSelectorItem : DependencyObjectSelectorItem<string, Brush>; |
| 187 | + |
| 188 | +public sealed class StringToBrushSelector : DependencyObjectSelector<string, Brush>; |
0 commit comments