|
| 1 | +// Copyright (c) 2024 .NET Foundation and Contributors. All rights reserved. |
| 2 | +// Licensed to the .NET Foundation under one or more agreements. |
| 3 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 4 | +// See the LICENSE file in the project root for full license information. |
| 5 | + |
| 6 | +using System.Linq.Expressions; |
| 7 | +using System.Text; |
| 8 | +using System.Windows; |
| 9 | +using System.Windows.Data; |
| 10 | +using System.Windows.Markup.Primitives; |
| 11 | +using System.Windows.Media; |
| 12 | +using DynamicData; |
| 13 | + |
| 14 | +namespace ReactiveUI.Wpf.Binding; |
| 15 | + |
| 16 | +internal class ValidationBindingWpf<TView, TViewModel, TVProp, TVMProp> : IReactiveBinding<TView, TVMProp> |
| 17 | + where TView : class, IViewFor |
| 18 | + where TViewModel : class |
| 19 | +{ |
| 20 | + private const string DotValue = "."; |
| 21 | + private readonly FrameworkElement _control; |
| 22 | + private readonly DependencyProperty? _dpPropertyName; |
| 23 | + private readonly TViewModel _viewModel; |
| 24 | + private readonly string? _vmPropertyName; |
| 25 | + private IDisposable? _inner; |
| 26 | + |
| 27 | + public ValidationBindingWpf( |
| 28 | + TView view, |
| 29 | + TViewModel viewModel, |
| 30 | + Expression<Func<TViewModel, TVMProp?>> vmProperty, |
| 31 | + Expression<Func<TView, TVProp>> viewProperty) |
| 32 | + { |
| 33 | + // Get the ViewModel details |
| 34 | + _viewModel = viewModel; |
| 35 | + ViewModelExpression = Reflection.Rewrite(vmProperty.Body); |
| 36 | + var vmet = ViewModelExpression.GetExpressionChain(); |
| 37 | + var vmFullName = vmet.Select(x => x.GetMemberInfo()?.Name).Aggregate(new StringBuilder(), (sb, x) => sb.Append(x).Append('.')).ToString(); |
| 38 | + if (vmFullName.EndsWith(DotValue)) |
| 39 | + { |
| 40 | + vmFullName = vmFullName.Substring(0, vmFullName.Length - 1); |
| 41 | + } |
| 42 | + |
| 43 | + _vmPropertyName = vmFullName; |
| 44 | + |
| 45 | + // Get the View details |
| 46 | + View = view; |
| 47 | + ViewExpression = Reflection.Rewrite(viewProperty.Body); |
| 48 | + var vet = ViewExpression.GetExpressionChain().ToArray(); |
| 49 | + var controlName = string.Empty; |
| 50 | + var index = vet.IndexOf(vet.Last()!); |
| 51 | + if (vet != null && index > 0) |
| 52 | + { |
| 53 | + controlName = vet[vet.IndexOf(vet.Last()!) - 1]!.GetMemberInfo()?.Name |
| 54 | + ?? throw new ArgumentException($"Control name not found on {typeof(TView).Name}"); |
| 55 | + } |
| 56 | + |
| 57 | + _control = FindControlsByName(view as DependencyObject, controlName).FirstOrDefault()!; |
| 58 | + var controlDpPropertyName = vet?.Last().GetMemberInfo()?.Name; |
| 59 | + _dpPropertyName = GetDependencyProperty(_control, controlDpPropertyName) ?? throw new ArgumentException($"Dependency property not found on {typeof(TVProp).Name}"); |
| 60 | + |
| 61 | + var somethingChanged = Reflection.ViewModelWhenAnyValue(viewModel, view, ViewModelExpression).Select(tvm => (TVMProp?)tvm).Merge( |
| 62 | + view.WhenAnyDynamic(ViewExpression, x => (TVProp?)x.Value).Select(p => default(TVMProp))); |
| 63 | + Changed = somethingChanged; |
| 64 | + Direction = BindingDirection.TwoWay; |
| 65 | + Bind(); |
| 66 | + } |
| 67 | + |
| 68 | + public System.Linq.Expressions.Expression ViewModelExpression { get; } |
| 69 | + |
| 70 | + public TView View { get; } |
| 71 | + |
| 72 | + public System.Linq.Expressions.Expression ViewExpression { get; } |
| 73 | + |
| 74 | + public IObservable<TVMProp?> Changed { get; } |
| 75 | + |
| 76 | + public BindingDirection Direction { get; } |
| 77 | + |
| 78 | + public IDisposable Bind() |
| 79 | + { |
| 80 | + _control.SetBinding(_dpPropertyName, new System.Windows.Data.Binding() |
| 81 | + { |
| 82 | + Source = _viewModel, |
| 83 | + Path = new(_vmPropertyName), |
| 84 | + Mode = BindingMode.TwoWay, |
| 85 | + UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged, |
| 86 | + }); |
| 87 | + |
| 88 | + _inner = Disposable.Create(() => BindingOperations.ClearBinding(_control, _dpPropertyName)); |
| 89 | + |
| 90 | + return _inner; |
| 91 | + } |
| 92 | + |
| 93 | + public void Dispose() |
| 94 | + { |
| 95 | + _inner?.Dispose(); |
| 96 | + GC.SuppressFinalize(this); |
| 97 | + } |
| 98 | + |
| 99 | + private static IEnumerable<DependencyProperty> EnumerateDependencyProperties(object element) |
| 100 | + { |
| 101 | + if (element != null) |
| 102 | + { |
| 103 | + var markupObject = MarkupWriter.GetMarkupObjectFor(element); |
| 104 | + if (markupObject != null) |
| 105 | + { |
| 106 | + foreach (var mp in markupObject.Properties) |
| 107 | + { |
| 108 | + if (mp.DependencyProperty != null) |
| 109 | + { |
| 110 | + yield return mp.DependencyProperty; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private static IEnumerable<DependencyProperty> EnumerateAttachedProperties(object element) |
| 118 | + { |
| 119 | + if (element != null) |
| 120 | + { |
| 121 | + var markupObject = MarkupWriter.GetMarkupObjectFor(element); |
| 122 | + if (markupObject != null) |
| 123 | + { |
| 124 | + foreach (var mp in markupObject.Properties) |
| 125 | + { |
| 126 | + if (mp.IsAttached) |
| 127 | + { |
| 128 | + yield return mp.DependencyProperty; |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private static DependencyProperty? GetDependencyProperty(object element, string? name) => |
| 136 | + EnumerateDependencyProperties(element).Concat(EnumerateAttachedProperties(element)).FirstOrDefault(x => x.Name == name); |
| 137 | + |
| 138 | + private static IEnumerable<FrameworkElement> FindControlsByName(DependencyObject? parent, string? name) |
| 139 | + { |
| 140 | + if (parent == null) |
| 141 | + { |
| 142 | + yield break; |
| 143 | + } |
| 144 | + |
| 145 | + if (name == null) |
| 146 | + { |
| 147 | + yield break; |
| 148 | + } |
| 149 | + |
| 150 | + var childCount = VisualTreeHelper.GetChildrenCount(parent); |
| 151 | + |
| 152 | + for (var i = 0; i < childCount; i++) |
| 153 | + { |
| 154 | + var child = VisualTreeHelper.GetChild(parent, i); |
| 155 | + |
| 156 | + if (child is FrameworkElement element && element.Name == name) |
| 157 | + { |
| 158 | + yield return element; |
| 159 | + } |
| 160 | + |
| 161 | + foreach (var descendant in FindControlsByName(child, name)) |
| 162 | + { |
| 163 | + yield return descendant; |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | +} |
0 commit comments