-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathBindingEvaluator.cs
47 lines (43 loc) · 1.62 KB
/
BindingEvaluator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Data;
namespace AmCharts.Windows.QuickCharts
{
/// <summary>
/// Utility class to facilitate temporary binding evaluation
/// </summary>
public class BindingEvaluator : FrameworkElement
{
/// <summary>
/// Created binding evaluator and set path to the property which's value should be evaluated.
/// </summary>
/// <param name="propertyPath">Path to the property</param>
public BindingEvaluator(string propertyPath)
{
_propertyPath = propertyPath;
}
private string _propertyPath;
/// <summary>
/// Dependency property used to evaluate values.
/// </summary>
public static readonly DependencyProperty EvaluatorProperty = DependencyProperty.Register(
"Evaluator", typeof(object), typeof(BindingEvaluator), null);
/// <summary>
/// Returns evaluated value of property on provided object source.
/// </summary>
/// <param name="source">Object for which property value should be evaluated</param>
/// <returns>Value of the property</returns>
public object Eval(object source)
{
ClearValue(BindingEvaluator.EvaluatorProperty);
var binding = new Binding(_propertyPath);
binding.Mode = BindingMode.OneTime;
binding.Source = source;
SetBinding(BindingEvaluator.EvaluatorProperty, binding);
return GetValue(BindingEvaluator.EvaluatorProperty);
}
}
}