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>
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 have a context menu which is binded to a list of items. When I click on an item, the command executes however I am having problems passing the selected item as a parameter, can anyone identify what I'm doing wrong?
I have tried passing {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}} as the command parameter however it just gives me the whole list within the context menu and when. When I tried {Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}} no value was seen.
<ContextMenu x:Key="SelectFileTab" ItemsSource="{Binding ContextMenuItems}" x:Name="contextmenu">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding}">
</MenuItem>
</DataTemplate>
</ContextMenu.ItemTemplate>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseUp">
<i:InvokeCommandAction Command="{Binding SelectedFileToAdd, Mode=OneWay}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ContextMenu>
I really over complicated something quite simple, there was no need to use interaction.triggers issue to the command, all I needed to do was
<ContextMenu x:Key="SelectFileTab" ItemsSource="{Binding ContextMenuItems}" x:Name="contextmenu">
<ContextMenu.ItemTemplate>
<DataTemplate>
<MenuItem Header="{Binding}" Command="{Binding RelativeSource={RelativeSource AncestorType=Window}, Path=DataContext.SelectedFileToAdd}"
CommandParameter="{Binding RelativeSource={RelativeSource Self}}"/>
</DataTemplate>
</ContextMenu.ItemTemplate>
</ContextMenu>
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>
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>