1#if !NETFX_CORE && !XAMARIN
11using System.Collections.Generic;
12using System.Diagnostics;
15using System.Windows.Controls;
17using System.ComponentModel;
32 Visibility = System.Windows.Visibility.Collapsed;
40 private void CallMethod(
object sender, EventArgs e)
42 object target = this.DataContext;
43 var cvs = target as System.Windows.Data.CollectionViewSource;
44 if (cvs !=
null && cvs.View !=
null)
46 target = cvs.View.CurrentItem;
50 var icv = target as ICollectionView;
52 target = icv.CurrentItem;
54 if (target ==
null)
return;
56 var targetMethod = target.GetType().GetMethod(
MethodName);
57 if (targetMethod ==
null)
61 Trace.TraceError(
"Csla.Xaml.TriggerAction Error: CallMethod path error: '{0}' method not found on '{1}', DataContext '{2}'",
MethodName, target.GetType(),
this.DataContext.GetType());
66 var pCount = targetMethod.GetParameters().Length;
69 targetMethod.Invoke(target,
null);
73 object parameterValue =
null;
75 parameterValue = GetMethodParameter();
79 targetMethod.Invoke(target,
new object[] {
this,
new ExecuteEventArgs
90 private void HookEvent(FrameworkElement oldTarget,
string oldEvent, FrameworkElement newTarget,
string newEvent)
92 if (!ReferenceEquals(oldTarget, newTarget) || oldEvent != newEvent)
94 if (oldTarget !=
null && !
string.IsNullOrEmpty(oldEvent))
96 var eventRef = oldTarget.GetType().GetEvent(oldEvent);
99 var invoke = eventRef.EventHandlerType.GetMethod(
"Invoke");
100 var p = invoke.GetParameters();
103 var p1Type = p[1].ParameterType;
104 if (typeof(EventArgs).IsAssignableFrom(p1Type))
106 var del = Delegate.CreateDelegate(eventRef.EventHandlerType,
108 this.GetType().GetMethod(
"CallMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic));
109 eventRef.RemoveEventHandler(oldTarget, del);
121 if (newTarget !=
null && !
string.IsNullOrEmpty(newEvent))
123 var eventRef = newTarget.GetType().GetEvent(newEvent);
124 if (eventRef !=
null)
126 var invoke = eventRef.EventHandlerType.GetMethod(
"Invoke");
127 var p = invoke.GetParameters();
130 var p1Type = p[1].ParameterType;
131 if (typeof(EventArgs).IsAssignableFrom(p1Type))
133 var del = Delegate.CreateDelegate(eventRef.EventHandlerType,
135 this.GetType().GetMethod(
"CallMethod", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.NonPublic));
136 eventRef.AddEventHandler(newTarget, del);
156 DependencyProperty.Register(
"TargetControl", typeof(FrameworkElement),
161 (FrameworkElement)e.OldValue, ta.TriggerEvent, (FrameworkElement)e.NewValue, ta.TriggerEvent);
178 DependencyProperty.Register(
"TriggerEvent", typeof(
string),
179 typeof(
TriggerAction),
new PropertyMetadata(
"Click", (o, e) =>
182 ta.HookEvent(ta.TargetControl, (
string)e.OldValue, ta.TargetControl, (
string)e.NewValue);
200 DependencyProperty.Register(
"MethodName", typeof(
string),
218 DependencyProperty.Register(
"MethodParameter", typeof(
object),
237 DependencyProperty.Register(
"RebindParameterDynamically", typeof(
bool),
253#region GetMethodParameter
255 private object GetMethodParameter()
258 if (be !=
null && be.ParentBinding !=
null)
260 var newBinding = CopyBinding(be.ParentBinding);
266 private static System.Windows.Data.Binding CopyBinding(System.Windows.Data.Binding oldBinding)
268 var result =
new System.Windows.Data.Binding();
269 result.BindsDirectlyToSource = oldBinding.BindsDirectlyToSource;
270 result.Converter = oldBinding.Converter;
271 result.ConverterCulture = oldBinding.ConverterCulture;
272 result.ConverterParameter = oldBinding.ConverterParameter;
273 result.Mode = oldBinding.Mode;
274 result.NotifyOnValidationError = oldBinding.NotifyOnValidationError;
275 result.Path = oldBinding.Path;
276 if (oldBinding.ElementName !=
null)
277 result.ElementName = oldBinding.ElementName;
278 else if (oldBinding.RelativeSource !=
null)
279 result.RelativeSource = oldBinding.RelativeSource;
281 result.Source = oldBinding.Source;
282 result.UpdateSourceTrigger = oldBinding.UpdateSourceTrigger;
283 result.ValidatesOnExceptions = oldBinding.ValidatesOnExceptions;
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.
Control used to invoke a method on the DataContext based on an event being raised by a UI control.
static readonly DependencyProperty TriggerEventProperty
Gets or sets the name of the event that will trigger the action.
string MethodName
Gets or sets the name of the method to be invoked.
static readonly DependencyProperty MethodParameterProperty
Gets or sets the value of a parameter to be passed to the invoked method.
bool RebindParameterDynamically
Gets or sets a value indicating whether the MethodParameter value should be dynamically rebound befor...
TriggerAction()
Creates an instance of the object.
FrameworkElement TargetControl
Gets or sets the target UI control.
static readonly DependencyProperty TargetControlProperty
Gets or sets the target UI control.
static readonly DependencyProperty RebindParameterDynamicallyProperty
Gets or sets a value indicating whether the MethodParameter value should be dynamically rebound befor...
object MethodParameter
Gets or sets the value of a parameter to be passed to the invoked method.
static readonly DependencyProperty MethodNameProperty
Gets or sets the name of the method to be invoked.
string TriggerEvent
Gets or sets the name of the event that will trigger the action.