|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | +using System.Windows; |
| 7 | +using System.Windows.Data; |
| 8 | + |
| 9 | +namespace MahApps.Metro.Controls.Helper |
| 10 | +{ |
| 11 | + /// <summary> |
| 12 | + /// A helper class to evaluate Bindings in code behind |
| 13 | + /// </summary> |
| 14 | + public static class BindingHelper |
| 15 | + { |
| 16 | + /// <summary> |
| 17 | + /// A dummy property to initialize the binding to evaluate |
| 18 | + /// </summary> |
| 19 | + private static readonly DependencyProperty DummyProperty = DependencyProperty.RegisterAttached( |
| 20 | + "Dummy", |
| 21 | + typeof(object), |
| 22 | + typeof(BindingHelper), |
| 23 | + new UIPropertyMetadata(null)); |
| 24 | + |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Evaluates a defined <see cref="Binding"/>-path on the given object |
| 28 | + /// </summary> |
| 29 | + /// <param name="source">the object to evaluate</param> |
| 30 | + /// <param name="expression">the binding expression to evaluate</param> |
| 31 | + /// <returns>the result of the <see cref="Binding"/></returns> |
| 32 | + public static object Eval(object source, string expression) |
| 33 | + { |
| 34 | + Binding binding = new Binding(expression) { Source = source }; |
| 35 | + return Eval(binding); |
| 36 | + } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Evaluates a defined <see cref="Binding"/>-path on the given object |
| 40 | + /// </summary> |
| 41 | + /// <param name="source">the object to evaluate</param> |
| 42 | + /// <param name="expression">the binding expression to evaluate</param> |
| 43 | + /// <param name="format">the stringformat to use</param> |
| 44 | + /// <returns>the result of the <see cref="Binding"/></returns> |
| 45 | + public static object Eval(object source, string expression, string format) |
| 46 | + { |
| 47 | + Binding binding = new Binding(expression) { Source = source, StringFormat = format }; |
| 48 | + return Eval(binding); |
| 49 | + } |
| 50 | + |
| 51 | + /// <summary> |
| 52 | + /// Evaluates a defined <see cref="Binding"/> on the given object |
| 53 | + /// </summary> |
| 54 | + /// <param name="binding">The <see cref="Binding"/> to evaluate</param> |
| 55 | + /// <param name="source">the object to evaluate</param> |
| 56 | + /// <returns></returns> |
| 57 | + public static object Eval(Binding binding, object source) |
| 58 | + { |
| 59 | + if (binding is null) throw new ArgumentNullException(nameof(binding)); |
| 60 | + |
| 61 | + Binding newBinding = new Binding() |
| 62 | + { |
| 63 | + Source = source, |
| 64 | + AsyncState = binding.AsyncState, |
| 65 | + BindingGroupName = binding.BindingGroupName, |
| 66 | + BindsDirectlyToSource = binding.BindsDirectlyToSource, |
| 67 | + Path = binding.Path, |
| 68 | + Converter = binding.Converter, |
| 69 | + ConverterCulture = binding.ConverterCulture, |
| 70 | + ConverterParameter = binding.ConverterParameter, |
| 71 | + FallbackValue = binding.FallbackValue, |
| 72 | + IsAsync = binding.IsAsync, |
| 73 | + Mode = BindingMode.OneWay, |
| 74 | + StringFormat = binding.StringFormat, |
| 75 | + TargetNullValue = binding.TargetNullValue |
| 76 | + }; |
| 77 | + return Eval(newBinding); |
| 78 | + } |
| 79 | + |
| 80 | + /// <summary> |
| 81 | + /// Evaluates a defined <see cref="Binding"/> on the given <see cref="DependencyObject"/> |
| 82 | + /// </summary> |
| 83 | + /// <param name="binding">The <see cref="Binding"/> to evaluate</param> |
| 84 | + /// <param name="dependencyObject">optional: The <see cref="DependencyObject"/> to evalutate</param> |
| 85 | + /// <returns></returns> |
| 86 | + public static object Eval(Binding binding, DependencyObject dependencyObject = null) |
| 87 | + { |
| 88 | + dependencyObject ??= new DependencyObject(); |
| 89 | + BindingOperations.SetBinding(dependencyObject, DummyProperty, binding); |
| 90 | + return dependencyObject.GetValue(DummyProperty); |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments