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>
Related
How Command parameter can be bind to the selected index in ListBox? My code:
<ListBox>
<i:Interaction.Triggers>
<i:EventTrigger
EventName="MouseDoubleClick">
<i:InvokeCommandAction
Command="{
Binding Path=SelectPath,
Mode=OneTime,
RelativeSource={
RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}
}
}"
CommandParameter="ListBox.SelectedIndex" // how can this parameter be bind to SelectedIndex
/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
EDIT:
What I did and it works: created new dp property SelectedListBoxIndex bind it with SelectedIndex and then pass property as command parameter. But is there a better way to do it?
<ListBox
SelectedIndex="{
Binding Path=SelectedListBoxIndex,
RelativeSource={
RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}
}}">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="MouseDoubleClick">
<i:InvokeCommandAction
Command="{
Binding Path=SelectPath,
Mode=OneTime,
RelativeSource={
RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}
}
}"
CommandParameter="{
Binding Path=SelectedListBoxIndex,
RelativeSource={
RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}
}
}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
One way could be setting your ListBox x:Name and then binding CommandParameter to its SelectedIndex property like this:
<ListBox x:Name="lb">
<i:Interaction.Triggers>
<i:EventTrigger
EventName="MouseDoubleClick">
<i:InvokeCommandAction
Command="{
Binding Path=SelectPath,
Mode=OneTime,
RelativeSource={
RelativeSource Mode=FindAncestor,
AncestorType={x:Type UserControl}
}
}"
CommandParameter="{Binding ElementName=lb, Path=SelectedIndex}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
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>
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>