InvokeCommandBehavior Class |
Namespace: WeroSoft.Windows.Controls
public class InvokeCommandBehavior : Behavior<FrameworkElement>
The WPF is built to bind commands to controls implementing the ICommandSource interface. This interface provides the popular Command and CommandParameter properties. The MVVM pattern targets the goal that no code behind is written in a view. Unfortunately this is not easy if events are used in the controls because events are not routed to commands because the command source dos not support this behavior.
This type provides a solution for this dilemma. It implements a framework behavior which allows connecting the event raising with a command execution by registering a behavior to the appropriate control using the interaction interface of the WPF.
This sample demonstrates the registration of the behavior in the XAML (first code samples). The sample assumes that the used view model provides the appropriate command, whose code is shown as second part.
The sample uses the following XML name spaces in XAML code definition:
XML name space | Definition |
---|---|
view | http://schemas.weroSoft.net/xaml/controls |
i | http://schemas.microsoft.com/expression/2010/interactivity |
wsc | http://schemas.weroSoft.net/xaml/controls |
<GroupBox Grid.Row="1" Header="Sliding pages"> <view:SlidingPageControl Grid.Row="4" Grid.ColumnSpan="6" Name="SlidingPageControl" ObjectCollection="{Binding SlidingPageCollection, Mode=TwoWay}" DefaultScrollMode="{Binding DefaultScrollMode}" CustomConfiguration="{Binding TestConfiguration}" SlidingPageHiddenAction="SlidingPageHiddenAction" SlidingPageLoadedAction="SlidingPageLoadedAction" SlidingPageShownAction="SlidingPageShownAction" SlidingPageUnloadedAction="SlidingPageUnloadedAction" > <i:Interaction.Behaviors> <wsc:InvokeCommandBehavior Event="SlidingPageValidateAction" Command="{Binding SlidingPageValidateCommand}" PassArguments="True" /> </i:Interaction.Behaviors> </view:SlidingPageControl> </GroupBox>
public class SlidingPageValidateCommand : ICommand { public event EventHandler CanExecuteChanged = delegate { }; public bool CanExecute(object parameter) { return true; } public void Execute(object parameter) { // Check whether the parameter has the correct type to access the // needed information. if (parameter is EventArgsWrapper wrapper && wrapper.EventArgs is SlidingPageValidateEventArgs slidingPageEventArgs) { // Get the view model of the sender. SlidingPageDialogViewModel viewModel = wrapper.GetSenderViewModel<SlidingPageDialogViewModel>(); // Add your decision code here. // ... if (viewModel.DisablePageChange) { // If changing the sliding page shall be avoided, // set the cancel event here. slidingPageEventArgs.Cancel = true; } } } }
The InvokeCommandBehavior type exposes the following members.
Name | Description | |
---|---|---|
InvokeCommandBehavior | Initializes a new instance of the InvokeCommandBehavior class |
Name | Description | |
---|---|---|
AssociatedObject | (Inherited from BehaviorFrameworkElement.) | |
AssociatedType | (Inherited from Behavior.) | |
CanFreeze | Gets a value that indicates whether the object can be made unmodifiable. (Inherited from Freezable.) | |
Command |
Gets or sets the command to execute.
| |
DependencyObjectType | Gets the DependencyObjectType that wraps the CLR type of this instance. (Inherited from DependencyObject.) | |
Dispatcher | Gets the Dispatcher this DispatcherObject is associated with. (Inherited from DispatcherObject.) | |
Event |
Gets or sets the event to handle.
| |
HasAnimatedProperties | Gets a value that indicates whether one or more AnimationClock objects is associated with any of this object's dependency properties. (Inherited from Animatable.) | |
IsFrozen | Gets a value that indicates whether the object is currently modifiable. (Inherited from Freezable.) | |
IsSealed | Gets a value that indicates whether this instance is currently sealed (read-only). (Inherited from DependencyObject.) | |
PassArguments |
Gets or sets whether the arguments shall be passed to the command or not.
| |
Sender |
Gets or sets the sender of the action.
|
Name | Description | |
---|---|---|
ApplyAnimationClock(DependencyProperty, AnimationClock) | Applies an AnimationClock to the specified DependencyProperty. If the property is already animated, the SnapshotAndReplace handoff behavior is used. (Inherited from Animatable.) | |
ApplyAnimationClock(DependencyProperty, AnimationClock, HandoffBehavior) | Applies an AnimationClock to the specified DependencyProperty. If the property is already animated, the specified HandoffBehavior is used. (Inherited from Animatable.) | |
Attach | (Inherited from Behavior.) | |
BeginAnimation(DependencyProperty, AnimationTimeline) | Applies an animation to the specified DependencyProperty. The animation is started when the next frame is rendered. If the specified property is already animated, the SnapshotAndReplace handoff behavior is used. (Inherited from Animatable.) | |
BeginAnimation(DependencyProperty, AnimationTimeline, HandoffBehavior) | Applies an animation to the specified DependencyProperty. The animation is started when the next frame is rendered. If the specified property is already animated, the specified HandoffBehavior is used. (Inherited from Animatable.) | |
ClearValue(DependencyProperty) | Clears the local value of a property. The property to be cleared is specified by a DependencyProperty identifier. (Inherited from DependencyObject.) | |
ClearValue(DependencyPropertyKey) | Clears the local value of a read-only property. The property to be cleared is specified by a DependencyPropertyKey. (Inherited from DependencyObject.) | |
Clone | Creates a modifiable clone of this Animatable, making deep copies of this object's values. When copying this object's dependency properties, this method copies resource references and data bindings (but they might no longer resolve) but not animations or their current values. (Inherited from Animatable.) | |
CloneCore | Makes the instance a clone (deep copy) of the specified Freezable using base (non-animated) property values. (Inherited from Freezable.) | |
CloneCurrentValue | Creates a modifiable clone (deep copy) of the Freezable using its current values. (Inherited from Freezable.) | |
CloneCurrentValueCore | Makes the instance a modifiable clone (deep copy) of the specified Freezable using current property values. (Inherited from Freezable.) | |
CoerceValue | Coerces the value of the specified dependency property. This is accomplished by invoking any CoerceValueCallback function specified in property metadata for the dependency property as it exists on the calling DependencyObject. (Inherited from DependencyObject.) | |
CreateInstance | Initializes a new instance of the Freezable class. (Inherited from Freezable.) | |
CreateInstanceCore | (Inherited from Behavior.) | |
Detach | (Inherited from Behavior.) | |
Equals | Determines whether a provided DependencyObject is equivalent to the current DependencyObject. (Inherited from DependencyObject.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
Freeze | Makes the current object unmodifiable and sets its IsFrozen property to true. (Inherited from Freezable.) | |
FreezeCore | Makes this Animatable object unmodifiable or determines whether it can be made unmodifiable. (Inherited from Animatable.) | |
GetAnimationBaseValue | Returns the non-animated value of the specified DependencyProperty. (Inherited from Animatable.) | |
GetAsFrozen | Creates a frozen copy of the Freezable, using base (non-animated) property values. Because the copy is frozen, any frozen sub-objects are copied by reference. (Inherited from Freezable.) | |
GetAsFrozenCore | Makes the instance a frozen clone of the specified Freezable using base (non-animated) property values. (Inherited from Freezable.) | |
GetCommandParameter |
Returns the parameter of the command.
| |
GetCurrentValueAsFrozen | Creates a frozen copy of the Freezable using current property values. Because the copy is frozen, any frozen sub-objects are copied by reference. (Inherited from Freezable.) | |
GetCurrentValueAsFrozenCore | Makes the current instance a frozen clone of the specified Freezable. If the object has animated dependency properties, their current animated values are copied. (Inherited from Freezable.) | |
GetHashCode | Gets a hash code for this DependencyObject. (Inherited from DependencyObject.) | |
GetLocalValueEnumerator | Creates a specialized enumerator for determining which dependency properties have locally set values on this DependencyObject. (Inherited from DependencyObject.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
GetValue | Returns the current effective value of a dependency property on this instance of a DependencyObject. (Inherited from DependencyObject.) | |
InvalidateProperty | Re-evaluates the effective value for the specified dependency property (Inherited from DependencyObject.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
OnAttached |
Event handler on attaching the behavior to the control.
(Overrides Behavior.OnAttached.) | |
OnChanged | Called when the current Freezable object is modified. (Inherited from Freezable.) | |
OnDetaching | (Inherited from Behavior.) | |
OnFreezablePropertyChanged(DependencyObject, DependencyObject) | Ensures that appropriate context pointers are established for a DependencyObjectType data member that has just been set. (Inherited from Freezable.) | |
OnFreezablePropertyChanged(DependencyObject, DependencyObject, DependencyProperty) | This member supports the Windows Presentation Foundation (WPF) infrastructure and is not intended to be used directly from your code. (Inherited from Freezable.) | |
OnPropertyChanged | Overrides the DependencyObject implementation of OnPropertyChanged(DependencyPropertyChangedEventArgs) to also invoke any Changed handlers in response to a changing dependency property of type Freezable. (Inherited from Freezable.) | |
ReadLocalValue | Returns the local value of a dependency property, if it exists. (Inherited from DependencyObject.) | |
ReadPreamble | Ensures that the Freezable is being accessed from a valid thread. Inheritors of Freezable must call this method at the beginning of any API that reads data members that are not dependency properties. (Inherited from Freezable.) | |
SetCurrentValue | Sets the value of a dependency property without changing its value source. (Inherited from DependencyObject.) | |
SetValue(DependencyProperty, Object) | Sets the local value of a dependency property, specified by its dependency property identifier. (Inherited from DependencyObject.) | |
SetValue(DependencyPropertyKey, Object) | Sets the local value of a read-only dependency property, specified by the DependencyPropertyKey identifier of the dependency property. (Inherited from DependencyObject.) | |
ShouldSerializeProperty | Returns a value that indicates whether serialization processes should serialize the value for the provided dependency property. (Inherited from DependencyObject.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
WritePostscript | Raises the Changed event for the Freezable and invokes its OnChanged method. Classes that derive from Freezable should call this method at the end of any API that modifies class members that are not stored as dependency properties. (Inherited from Freezable.) | |
WritePreamble | Verifies that the Freezable is not frozen and that it is being accessed from a valid threading context. Freezable inheritors should call this method at the beginning of any API that writes to data members that are not dependency properties. (Inherited from Freezable.) |
Name | Description | |
---|---|---|
Changed | Occurs when the Freezable or an object it contains is modified. (Inherited from Freezable.) |
Name | Description | |
---|---|---|
CommandProperty |
The DependencyProperty of Command.
| |
EventProperty |
The DependencyProperty of Event.
| |
PassArgumentsProperty |
The DependencyProperty of PassArguments.
| |
SenderProperty |
The DependencyProperty of Sender.
|
Name | Description | |
---|---|---|
Clone(CloneWorkingMode, IEnumerableType) | Overloaded.
Clones an object by serialization.
(Defined by RuntimeExtensions.) | |
CloneT(CloneWorkingMode, IEnumerableType) | Overloaded.
Clones an object by serialization.
(Defined by RuntimeExtensions.) | |
Convert | (Defined by DataConverterExtensions.) | |
CreateTraceDataEntry(StringBuilder, SourceLevels, TraceableKind, String) | Overloaded.
Creates a data entry into a string builder according to the giving parameters.
(Defined by TraceExtensions.) | |
CreateTraceDataEntry(SourceLevels, TraceableKind, String, String, Int32) | Overloaded.
Creates a trace entry for a data element.
(Defined by TraceExtensions.) | |
CreateTraceDataEntry(StringBuilder, SourceLevels, TraceableKind, String, String, Int32) | Overloaded.
Creates a trace entry into a string builder.
(Defined by TraceExtensions.) | |
DynamicCast |
Dynamically casts a reference to another type.
(Defined by TypeManagerHelper.) | |
ExtractSystemParametersFromTrigger |
Extracts the system parameters from a collection of trigger parameter and transform them to a dictionary using string keys and string values.
(Defined by TriggerExtensions.) | |
GetFieldValue(String) | Overloaded.
Gets the data object held by a defined field of a particular type.
(Defined by UnitTest.) | |
GetFieldValueT(String) | Overloaded.
Gets the data object held by a defined field of a particular type.
(Defined by UnitTest.) | |
GetParentDependencyObjectOfType |
Extension method for dependency objects to resolve a parent of a defined type.
(Defined by WindowsHelper.) | |
GetPropertyValue(String) | Overloaded.
Gets the data object held by a defined property of a particular type.
(Defined by UnitTest.) | |
GetPropertyValueT(String) | Overloaded.
Gets the data object held by a defined property of a particular type.
(Defined by UnitTest.) | |
Inject(Type, Object) | Overloaded.
Injects all fields or properties which are marked with the defined attribute type name.
(Defined by DataInjection.) | |
Inject(Type, Object, Boolean) | Overloaded.
Injects all fields or properties which are marked with the defined attribute type name.
(Defined by DataInjection.) | |
InjectField |
Injects a value to a property within a data object.
(Defined by DataInjection.) | |
InjectProperty(String, Object) | Overloaded.
Injects a value to a property within a data object.
(Defined by DataInjection.) | |
InjectProperty(String, Type, Object) | Overloaded.
Injects a value to a property within a data object.
(Defined by DataInjection.) | |
Invoke(String, Object) | Overloaded.
Invokes a method on an object by reflection.
(Defined by UnitTest.) | |
InvokeTResult(String, Object) | Overloaded.
Invokes a method on an object by reflection.
(Defined by UnitTest.) | |
SetFieldValue |
Sets the data object on a defined field of a particular type.
(Defined by UnitTest.) | |
SetPropertyValue |
Sets the data object on a defined property of a particular type.
(Defined by UnitTest.) | |
SetReadOnlyFieldValue |
Sets the data object on a defined field of a particular type.
(Defined by UnitTest.) | |
TryInjectProperty |
Tries to Inject a value to a property within a data object.
(Defined by DataInjection.) |