|
| 1 | +usingSystem; |
| 2 | +usingSystem.Linq; |
| 3 | +usingWindows.Foundation.Collections; |
| 4 | +usingWindows.UI.Xaml.Markup; |
| 5 | +usingMicrosoft.UI.Xaml; |
| 6 | +usingMicrosoft.UI.Xaml.Data; |
| 7 | +usingMicrosoft.UI.Xaml.Media; |
| 8 | + |
| 9 | +namespaceCoder.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 | +publicclassDependencyObjectSelectorItem<TK,TV>:DependencyObject |
| 23 | +whereTK:IEquatable<TK> |
| 24 | +{ |
| 25 | +publicstaticreadonlyDependencyPropertyKeyProperty= |
| 26 | +DependencyProperty.Register(nameof(Key), |
| 27 | +typeof(TK?), |
| 28 | +typeof(DependencyObjectSelectorItem<TK,TV>), |
| 29 | +newPropertyMetadata(null)); |
| 30 | + |
| 31 | +publicstaticreadonlyDependencyPropertyValueProperty= |
| 32 | +DependencyProperty.Register(nameof(Value), |
| 33 | +typeof(TV?), |
| 34 | +typeof(DependencyObjectSelectorItem<TK,TV>), |
| 35 | +newPropertyMetadata(null)); |
| 36 | + |
| 37 | +publicTK?Key |
| 38 | +{ |
| 39 | +get=>(TK?)GetValue(KeyProperty); |
| 40 | +set=>SetValue(KeyProperty,value); |
| 41 | +} |
| 42 | + |
| 43 | +publicTV?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 | +publicclassDependencyObjectSelector<TK,TV>:DependencyObject |
| 60 | +whereTK:IEquatable<TK> |
| 61 | +{ |
| 62 | +publicstaticreadonlyDependencyPropertyReferencesProperty= |
| 63 | +DependencyProperty.Register(nameof(References), |
| 64 | +typeof(DependencyObjectCollection), |
| 65 | +typeof(DependencyObjectSelector<TK,TV>), |
| 66 | +newPropertyMetadata(null,ReferencesPropertyChanged)); |
| 67 | + |
| 68 | +publicstaticreadonlyDependencyPropertySelectedKeyProperty= |
| 69 | +DependencyProperty.Register(nameof(SelectedKey), |
| 70 | +typeof(TK?), |
| 71 | +typeof(DependencyObjectSelector<TK,TV>), |
| 72 | +newPropertyMetadata(null,SelectedKeyPropertyChanged)); |
| 73 | + |
| 74 | +publicstaticreadonlyDependencyPropertySelectedObjectProperty= |
| 75 | +DependencyProperty.Register(nameof(SelectedObject), |
| 76 | +typeof(TV?), |
| 77 | +typeof(DependencyObjectSelector<TK,TV>), |
| 78 | +newPropertyMetadata(null)); |
| 79 | + |
| 80 | +publicDependencyObjectCollection?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 | +varitems=value.OfType<DependencyObjectSelectorItem<TK,TV>>().ToArray(); |
| 89 | +varkeys=items.Select(i=>i.Key).Distinct().ToArray(); |
| 90 | +if(keys.Length!=value.Count) |
| 91 | +thrownewArgumentException("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 | +publicTK?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 | +publicTV?SelectedObject |
| 115 | +{ |
| 116 | +get=>(TV?)GetValue(SelectedObjectProperty); |
| 117 | +set=>SetValue(SelectedObjectProperty,value); |
| 118 | +} |
| 119 | + |
| 120 | +publicDependencyObjectSelector() |
| 121 | +{ |
| 122 | +References=[]; |
| 123 | +} |
| 124 | + |
| 125 | +privatevoidUpdateSelectedObject() |
| 126 | +{ |
| 127 | +if(References!=null) |
| 128 | +{ |
| 129 | +// Look for a matching item a matching key, or fallback to the null |
| 130 | +// key. |
| 131 | +varreferences=References.OfType<DependencyObjectSelectorItem<TK,TV>>().ToArray(); |
| 132 | +varitem=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(itemis notnull) |
| 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 | +newBinding |
| 147 | +{ |
| 148 | +Source=item, |
| 149 | +Path=newPropertyPath(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 | +privatestaticvoidReferencesPropertyChanged(DependencyObjectobj,DependencyPropertyChangedEventArgsargs) |
| 161 | +{ |
| 162 | +varself=objasDependencyObjectSelector<TK,TV>; |
| 163 | +if(self==null)return; |
| 164 | +varoldValue=args.OldValueasDependencyObjectCollection; |
| 165 | +if(oldValue!=null) |
| 166 | +oldValue.VectorChanged-=self.OnVectorChangedReferences; |
| 167 | +varnewValue=args.NewValueasDependencyObjectCollection; |
| 168 | +if(newValue!=null) |
| 169 | +newValue.VectorChanged+=self.OnVectorChangedReferences; |
| 170 | +} |
| 171 | + |
| 172 | +// Called when the References collection changes without being replaced. |
| 173 | +privatevoidOnVectorChangedReferences(IObservableVector<DependencyObject>sender,IVectorChangedEventArgsargs) |
| 174 | +{ |
| 175 | +UpdateSelectedObject(); |
| 176 | +} |
| 177 | + |
| 178 | +// Called when SelectedKey changes. |
| 179 | +privatestaticvoidSelectedKeyPropertyChanged(DependencyObjectobj,DependencyPropertyChangedEventArgsargs) |
| 180 | +{ |
| 181 | +varself=objasDependencyObjectSelector<TK,TV>; |
| 182 | +self?.UpdateSelectedObject(); |
| 183 | +} |
| 184 | +} |
| 185 | + |
| 186 | +publicsealedclassStringToBrushSelectorItem:DependencyObjectSelectorItem<string,Brush>; |
| 187 | + |
| 188 | +publicsealedclassStringToBrushSelector:DependencyObjectSelector<string,Brush>; |