|
| 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 | +public class DependencyObjectSelectorItem<TK, TV> : DependencyObject |
| 16 | + where TK : IEquatable<TK> |
| 17 | +{ |
| 18 | + public static readonly DependencyProperty KeyProperty = |
| 19 | + DependencyProperty.Register(nameof(Key), |
| 20 | + typeof(TK?), |
| 21 | + typeof(DependencyObjectSelectorItem<TK, TV>), |
| 22 | + new PropertyMetadata(null)); |
| 23 | + |
| 24 | + public static readonly DependencyProperty ValueProperty = |
| 25 | + DependencyProperty.Register(nameof(Value), |
| 26 | + typeof(TV?), |
| 27 | + typeof(DependencyObjectSelectorItem<TK, TV>), |
| 28 | + new PropertyMetadata(null)); |
| 29 | + |
| 30 | + public TK? Key |
| 31 | + { |
| 32 | + get => (TK?)GetValue(KeyProperty); |
| 33 | + set => SetValue(KeyProperty, value); |
| 34 | + } |
| 35 | + |
| 36 | + public TV? Value |
| 37 | + { |
| 38 | + get => (TV?)GetValue(ValueProperty); |
| 39 | + set => SetValue(ValueProperty, value); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +[ContentProperty(Name = nameof(References))] |
| 44 | +public class DependencyObjectSelector<TK, TV> : DependencyObject |
| 45 | + where TK : IEquatable<TK> |
| 46 | +{ |
| 47 | + public static readonly DependencyProperty ReferencesProperty = |
| 48 | + DependencyProperty.Register(nameof(References), |
| 49 | + typeof(DependencyObjectCollection), |
| 50 | + typeof(DependencyObjectSelector<TK, TV>), |
| 51 | + new PropertyMetadata(null, ReferencesPropertyChanged)); |
| 52 | + |
| 53 | + public static readonly DependencyProperty SelectedKeyProperty = |
| 54 | + DependencyProperty.Register(nameof(SelectedKey), |
| 55 | + typeof(TK?), |
| 56 | + typeof(DependencyObjectSelector<TK, TV>), |
| 57 | + new PropertyMetadata(null, SelectedPropertyChanged)); |
| 58 | + |
| 59 | + public static readonly DependencyProperty SelectedObjectProperty = |
| 60 | + DependencyProperty.Register(nameof(SelectedObject), |
| 61 | + typeof(TV?), |
| 62 | + typeof(DependencyObjectSelector<TK, TV>), |
| 63 | + new PropertyMetadata(null)); |
| 64 | + |
| 65 | + public DependencyObjectCollection? References |
| 66 | + { |
| 67 | + get => (DependencyObjectCollection?)GetValue(ReferencesProperty); |
| 68 | + set |
| 69 | + { |
| 70 | + // Ensure unique keys and that the values are DependencyObjectSelectorItem<K, V>. |
| 71 | + if (value != null) |
| 72 | + { |
| 73 | + var items = value.OfType<DependencyObjectSelectorItem<TK, TV>>().ToArray(); |
| 74 | + var keys = items.Select(i => i.Key).Distinct().ToArray(); |
| 75 | + if (keys.Length != value.Count) |
| 76 | + throw new ArgumentException("ObservableCollection Keys must be unique."); |
| 77 | + } |
| 78 | + |
| 79 | + SetValue(ReferencesProperty, value); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public TK? SelectedKey |
| 84 | + { |
| 85 | + get => (TK?)GetValue(SelectedKeyProperty); |
| 86 | + set => SetValue(SelectedKeyProperty, value); |
| 87 | + } |
| 88 | + |
| 89 | + public TV? SelectedObject |
| 90 | + { |
| 91 | + get => (TV?)GetValue(SelectedObjectProperty); |
| 92 | + set => SetValue(SelectedObjectProperty, value); |
| 93 | + } |
| 94 | + |
| 95 | + public DependencyObjectSelector() |
| 96 | + { |
| 97 | + References = []; |
| 98 | + } |
| 99 | + |
| 100 | + private void OnVectorChangedReferences(IObservableVector<DependencyObject> sender, IVectorChangedEventArgs args) |
| 101 | + { |
| 102 | + UpdateSelectedObject(); |
| 103 | + } |
| 104 | + |
| 105 | + private void UpdateSelectedObject() |
| 106 | + { |
| 107 | + if (References != null) |
| 108 | + { |
| 109 | + var references = References.OfType<DependencyObjectSelectorItem<TK, TV>>().ToArray(); |
| 110 | + var item = references |
| 111 | + .FirstOrDefault(i => |
| 112 | + (i.Key == null && SelectedKey == null) || |
| 113 | + (i.Key != null && SelectedKey != null && i.Key!.Equals(SelectedKey!))) |
| 114 | + ?? references.FirstOrDefault(i => i.Key == null); |
| 115 | + if (item is not null) |
| 116 | + { |
| 117 | + BindingOperations.SetBinding |
| 118 | + ( |
| 119 | + this, |
| 120 | + SelectedObjectProperty, |
| 121 | + new Binding |
| 122 | + { |
| 123 | + Source = item, |
| 124 | + Path = new PropertyPath(nameof(DependencyObjectSelectorItem<TK, TV>.Value)), |
| 125 | + } |
| 126 | + ); |
| 127 | + return; |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + ClearValue(SelectedObjectProperty); |
| 132 | + } |
| 133 | + |
| 134 | + private static void ReferencesPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) |
| 135 | + { |
| 136 | + var self = obj as DependencyObjectSelector<TK, TV>; |
| 137 | + if (self == null) return; |
| 138 | + var oldValue = args.OldValue as DependencyObjectCollection; |
| 139 | + if (oldValue != null) |
| 140 | + oldValue.VectorChanged -= self.OnVectorChangedReferences; |
| 141 | + var newValue = args.NewValue as DependencyObjectCollection; |
| 142 | + if (newValue != null) |
| 143 | + newValue.VectorChanged += self.OnVectorChangedReferences; |
| 144 | + } |
| 145 | + |
| 146 | + private static void SelectedPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs args) |
| 147 | + { |
| 148 | + var self = obj as DependencyObjectSelector<TK, TV>; |
| 149 | + self?.UpdateSelectedObject(); |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +public sealed class StringToBrushSelectorItem : DependencyObjectSelectorItem<string, Brush>; |
| 154 | + |
| 155 | +public sealed class StringToBrushSelector : DependencyObjectSelector<string, Brush>; |
0 commit comments