I need to Pass PassEventArgsToCommand along with CommandParameter in Interaction.Triggers - WPF C#
My XAML Source Code:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<commandHelper:EventToCommand PassEventArgsToCommand="True" Command="{Binding OpenDetailsCommandByLeftMouseBtn}" CommandParameter="Provider"/>
</i:EventTrigger>
</i:Interaction.Triggers>
For me it throws an exception Invalid cast from 'System.String' to 'System.Windows.Input.MouseButtonEventArgs'.
You can use InvokeCommandAction.
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding ButtonClickCommand}" CommandParameter="parameter"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Related
How can i run a method from my code in the Interactions.trigger (interactivity) ?
I have code of
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction MethodName="HandleShowMessage"/>
</i:EventTrigger>
</i:Interaction.Triggers>
which is HandleShowMessage() is a method from the code behind.
I tried that code but its not working
You can binding by command like
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<ei:CallMethodAction Command="{Binding DataContext.HandleShowMessage, RelativeSource={RelativeSource AncestorType={x:Type expenses:MainWindow}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
I'm new to WPF and I don't know why binding from the viewmodel does not execute the specified command.
<CheckBox x:Name="CheckBoxName" Content="MyCheckBox" IsChecked="{Binding Restrictions.MyCheckBox}" Command="{Binding MyCommand}" CommandParameter="{Binding ElementName=CheckBoxName}"/>
When I check the checkbox in the view, MyCommand is executed as expected, but when I set the IsChecked property from the viewmodel using binding, the command is not executed.
Does anyone have any idea why it's happening? How can I execute the command from viewmodel using binding?
Thanks you very much!
As far as I know, the Command is executed on the "Click" event of the base "ButtonBase" class, which is not triggered if you change the IsChecked Property via Binding.
One solution would be to attach a trigger to the Checked and Unchecked event, which triggers your command.
First you need to add the namespace
xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
And for your checkbox
<CheckBox x:Name="CheckBoxName"
Content="MyCheckBox"
IsChecked="{Binding Restrictions.MyCheckBox}"
Command="{Binding MyCommand}"
CommandParameter="{Binding ElementName=CheckBoxName}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Checked">
<i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding Path=IsChecked, ElementName=CheckBoxName}" />
</i:EventTrigger>
<i:EventTrigger EventName="Unchecked">
<i:InvokeCommandAction Command="{Binding MyCommand}" CommandParameter="{Binding Path=IsChecked, ElementName=CheckBoxName}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</Checkbox>
The following Xaml code allows me to use a context menu in a listview and send the row details as an object to the view model:
<ListView.ContextMenu>
<ContextMenu>
<MenuItem Header="Link This Operation"
Command="{Binding SelectedOperation}"
CommandParameter="{Binding RelativeSource={RelativeSource AncestorType=ContextMenu}, Path=PlacementTarget.SelectedItem}"/>
</ContextMenu>
</ListView.ContextMenu>-->
However i want to do the same thing through a double mouse click. I have tried all sorts. Including this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding SelectedItem}"
CommandParameter="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=PlacementTarget.SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
This works but the object is always null.
Any ideas?
Try this one:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="{Binding SelectedOperation}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Intro
I'm working with WPF with MVVM-Light application
Goal
I have to invoke two commands from the same event, Is this possible using MVVM.?
Xaml look like this
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<command:EventToCommand Command="{Binding Command1}" PassEventArgsToCommand="False" />
<command:EventToCommand Command="{Binding Command2}" PassEventArgsToCommand="False" />
</i:EventTrigger>
</i:Interaction.Triggers>
Problem
When hooking two commands only one of them is invoked while triggering the event.
Q1 How to invoke two commands in a event?
I have heard about Composite commands in PRISM
For example, the CompositeCommand class is used in the Stock Trader Reference Implementation (Stock Trader RI) in order to implement the SubmitAllOrders command represented by the Submit All button in the buy/sell view. When the user clicks the Submit All button, each SubmitCommand defined by the individual buy/sell transactions is executed.
.
Q2 Is there anything like this in MVVM ?
Try using 2 event triggers:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Loaded">
<command:EventToCommand Command="{Binding Command1}" PassEventArgsToCommand="False" />
</i:EventTrigger>
<i:EventTrigger EventName="Loaded">
<command:EventToCommand Command="{Binding Command2}" PassEventArgsToCommand="False" />
</i:EventTrigger>
</i:Interaction.Triggers>
<StackPanel>
<TextBox Text="" x:Name="input"/>
<Button Content="Click">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click"><!-- TextBox has to contain "ABC" to get Button Click enabled this event-->
<i:InvokeCommandAction Command="{Binding OnAdd}" CommandParameter="1"></i:InvokeCommandAction>
</i:EventTrigger>
<i:EventTrigger EventName="Click"><!-- TextBox has to contain "123" to get Button Click enabled this event-->
<i:InvokeCommandAction Command="{Binding OnAdd2}" CommandParameter="1"></i:InvokeCommandAction>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
</StackPanel>
I want the <i:EventTrigger EventName="Click"> TextBox has to contain "ABC" to get Button Click enabled this event
I would suggest you do this handling within your OnAdd Command. Look especially for the CanExecute Method. This one handles if you Button will be enabled or not. You can read here how to implement this :
Commands Tutorial