-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLocBinding.cs
142 lines (120 loc) · 5.11 KB
/
LocBinding.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.ComponentModel;
using System.Globalization;
using System.Windows;
using System.Windows.Data;
using System.Windows.Markup;
namespace CodingSeb.Localization.WPF
{
/// <summary>
/// This is a localization dependant binding it recreate a standard binding and automatically update it when current language changed
/// </summary>
public class LocBinding : MarkupExtension
{
/// <summary>
/// Default Constructor
/// </summary>
public LocBinding()
{
WeakEventManager<Loc, CurrentLanguageChangedEventArgs>.AddHandler(Loc.Instance, nameof(Loc.Instance.CurrentLanguageChanged), CurrentLanguageChanged);
}
/// <summary>
/// Constructor
/// </summary>
/// <param name="path">The path to the binding source property.</param>
public LocBinding(PropertyPath path)
{
WeakEventManager<Loc, CurrentLanguageChangedEventArgs>.AddHandler(Loc.Instance, nameof(Loc.Instance.CurrentLanguageChanged), CurrentLanguageChanged);
Path = path;
}
~LocBinding()
{
WeakEventManager<Loc, CurrentLanguageChangedEventArgs>.RemoveHandler(Loc.Instance, nameof(Loc.Instance.CurrentLanguageChanged), CurrentLanguageChanged);
}
/// <summary>
/// Gets or sets the converter to use.
/// </summary>
public IValueConverter Converter { get; set; }
/// <summary>
/// Gets or sets the parameter to pass to the Converter.
/// </summary>
public object ConverterParameter { get; set; }
/// <summary>
/// Gets or sets the name of the element to use as the binding source object.
/// </summary>
public string ElementName { get; set; }
/// <summary>
/// Gets or sets the binding source by specifying its location relative to the position of the binding target.
/// </summary>
public RelativeSource RelativeSource { get; set; }
/// <summary>
/// Gets or sets the object to use as the binding source.
/// </summary>
public object Source { get; set; }
/// <summary>
/// Gets or sets a value that indicates whether to include the DataErrorValidationRule.
/// </summary>
public bool ValidatesOnDataErrors { get; set; }
/// <summary>
/// Gets or sets a value that indicates whether to include the ExceptionValidationRule.
/// </summary>
public bool ValidatesOnExceptions { get; set; }
/// <summary>
/// Gets or sets the path to the binding source property.
/// </summary>
[ConstructorArgument("path")]
public PropertyPath Path { get; set; }
/// <summary>
/// Gets or sets the culture in which to evaluate the converter.
/// </summary>
[TypeConverter(typeof(CultureInfoIetfLanguageTagConverter))]
public CultureInfo ConverterCulture { get; set; }
/// <summary>
/// The DependencyObject on which the binding is linked
/// </summary>
public DependencyObject DependencyObject { get; private set; }
/// <summary>
/// The DependencyProperty on which the binding is linked
/// </summary>
public DependencyProperty DependencyProperty { get; private set; }
/// <summary>
/// The internally created binding.
/// </summary>
public Binding Binding { get; set; }
public override object ProvideValue(IServiceProvider serviceProvider)
{
if (serviceProvider.GetService(typeof(IProvideValueTarget)) is not IProvideValueTarget service)
return this;
DependencyProperty = service.TargetProperty as DependencyProperty;
DependencyObject = service.TargetObject as DependencyObject;
if (DependencyObject == null || DependencyProperty == null)
{
return this;
}
if (Binding == null)
{
Binding = new Binding
{
Path = Path,
Converter = Converter,
ConverterCulture = ConverterCulture,
ConverterParameter = ConverterParameter
};
if (ElementName != null) Binding.ElementName = ElementName;
if (RelativeSource != null) Binding.RelativeSource = RelativeSource;
if (Source != null) Binding.Source = Source;
Binding.ValidatesOnDataErrors = ValidatesOnDataErrors;
Binding.ValidatesOnExceptions = ValidatesOnExceptions;
BindingOperations.SetBinding(DependencyObject, DependencyProperty, Binding);
}
return Binding.ProvideValue(serviceProvider);
}
private void CurrentLanguageChanged(object sender, CurrentLanguageChangedEventArgs e)
{
if (DependencyObject != null && DependencyProperty != null)
{
BindingOperations.GetBindingExpression(DependencyObject, DependencyProperty)?.UpdateTarget();
}
}
}
}