1#if !NETFX_CORE && !XAMARIN
11using System.Windows.Controls;
12using System.ComponentModel;
13using System.Collections.Generic;
25#region Attached properties
31 DependencyProperty.RegisterAttached(
"Target",
34 new PropertyMetadata(
null));
41 public static void SetTarget(UIElement ctrl,
object value)
56 var fe = ctrl as FrameworkElement;
58 result = fe.DataContext;
60 var icv = result as ICollectionView;
62 result = icv.CurrentItem;
70 DependencyProperty.RegisterAttached(
"MethodName",
73 new PropertyMetadata(
null));
99 DependencyProperty.RegisterAttached(
"TriggerEvent",
102 new PropertyMetadata((o, e) =>
104 var ctrl = o as UIElement;
134 DependencyProperty.RegisterAttached(
"MethodParameter",
137 new PropertyMetadata(
null));
158 private static System.Windows.Data.Binding CopyBinding(System.Windows.Data.Binding oldBinding)
160 var result =
new System.Windows.Data.Binding();
161 result.BindsDirectlyToSource = oldBinding.BindsDirectlyToSource;
162 result.Converter = oldBinding.Converter;
163 result.ConverterCulture = oldBinding.ConverterCulture;
164 result.ConverterParameter = oldBinding.ConverterParameter;
165 result.Mode = oldBinding.Mode;
166 result.NotifyOnValidationError = oldBinding.NotifyOnValidationError;
167 result.Path = oldBinding.Path;
168 if (oldBinding.ElementName !=
null)
169 result.ElementName = oldBinding.ElementName;
170 else if (oldBinding.RelativeSource !=
null)
171 result.RelativeSource = oldBinding.RelativeSource;
173 result.Source = oldBinding.Source;
174 result.UpdateSourceTrigger = oldBinding.UpdateSourceTrigger;
175 result.ValidatesOnExceptions = oldBinding.ValidatesOnExceptions;
181 private UIElement _element;
192 if (!
string.IsNullOrEmpty(triggerEvent))
195 var eventRef = ctrl.GetType().GetEvent(triggerEvent);
196 if (eventRef !=
null)
198 var invoke = eventRef.EventHandlerType.GetMethod(
"Invoke");
199 var p = invoke.GetParameters();
202 var p1Type = p[1].ParameterType;
203 if (typeof(EventArgs).IsAssignableFrom(p1Type))
205 var del = Delegate.CreateDelegate(eventRef.EventHandlerType,
207 this.GetType().GetMethod(
"CallMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic));
208 eventRef.AddEventHandler(ctrl, del);
221 private void CallMethod(
object sender, EventArgs e)
225 var targetMethod = target.GetType().GetMethod(methodName);
226 if (targetMethod ==
null)
227 throw new MissingMethodException(methodName);
230 var pCount = targetMethod.GetParameters().Length;
234 targetMethod.Invoke(target,
null);
235 else if (pCount == 2)
239 TriggerParameter = e,
240 TriggerSource = (FrameworkElement)_element
245 catch (System.Reflection.TargetInvocationException ex)
247 if (ex.InnerException !=
null)
248 throw ex.InnerException;
A strongly-typed resource class, for looking up localized strings, etc.
static string ExecuteBadParams
Looks up a localized string similar to Method to be executed must have 0 or 2 parameters.
static string ExecuteBadTriggerEvent
Looks up a localized string similar to Trigger event has an unsupported signature.
Arguments passed to a method invoked by the Execute trigger action.
Invokes a method on a target object when a trigger event is raised from the attached UI control.
InvokeMethod(UIElement ctrl)
Invokes the target method if all required attached property values have been set.
static readonly DependencyProperty TriggerEventProperty
Name of event raised by UI control that triggers invoking the target method.
static void SetMethodParameter(UIElement ctrl, object value)
Sets the parameter value to be passed to invoked method.
static void SetTriggerEvent(UIElement ctrl, string value)
Sets the name of event raised by UI control that triggers invoking the target method.
static void SetMethodName(UIElement ctrl, string value)
Sets the name of method to be invoked.
static void SetTarget(UIElement ctrl, object value)
Sets the object containing the method to be invoked.
static string GetMethodName(UIElement ctrl)
Gets the name of method to be invoked.
static object GetTarget(UIElement ctrl)
Gets the object containing the method to be invoked.
static readonly DependencyProperty TargetProperty
Object containing the method to be invoked.
static string GetTriggerEvent(UIElement ctrl)
Gets the name of event raised by UI control that triggers invoking the target method.
static object GetMethodParameter(UIElement ctrl)
Gets the parameter value to be passed to invoked method.
static readonly DependencyProperty MethodParameterProperty
Parameter value to be passed to invoked method.
static readonly DependencyProperty MethodNameProperty
Name of method to be invoked.