MVVMLight EventToCommand and passing event arguments - c#

I have a mousewheel interactivity trigger on a StackPanel:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseWheel">
<cmd:EventToCommand Command="{Binding DataContext.PreviousWeekCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=UserControl}}"
PassEventArgsToCommand="True"/>
</i:EventTrigger>
</i:Interaction.Triggers>
and it works great with the PreviousWeekCommand as follows (snippet):
_previousWeekCommand = new RelayCommand<object>(param => ShiftDays(-7), param => (true));
The PassEventArgsToCommand is there because I'm trying to switch this to a different command that will detect if the user has scrolled up or down. The problem is, after a lot of searching, I still can't figure out how to structure the command to deal with the args. Here's what I have, but it doesn't work:
_scrollWheelCommand = new RelayCommand<MouseEventArgs>(ScrollWheel, can => true);
and then this is the ScrollWheel declaration:
public void ScrollWheel(MouseEventArgs args)
Problem is, I never get to this method when I breakpoint it. I also don't know if I'm routing the arguments in the correct way.
EDIT: Oh, and I get no errors.

Try using MouseWheelEventArgs instead of MouseEventArgs when declaring your RelayCommand. The Parameter type should match the event in order for that to work.

Related

How to handle event of MainWindow in one of the ViewModel which belongs to a View, but not MainWindow

I wanted to handle MainWindow's Closing event in one of my ViewModel. This ViewModel belongs to a View which is displayed on top of the MainWindow. I wanted to save values of some properties present in that ViewModel while/before closing of the application.
I am using MvvmCross to implement MVVM pattern. So I tried overriding ViewDisappearing, ViewDisappeared and ViewDestroy methods, but they are only getting called when View get switched, but not when application/window closes.
While searching for this requirement, I could only find this answer: https://social.msdn.microsoft.com/Forums/vstudio/en-US/477e7e74-ccbf-4498-8ab9-ca2f3b836597/how-to-know-when-a-wpf-usercontrol-is-closing?forum=wpf , which is close to my requirement, but needs to be implemented in code-behind.
Can anyone please help me in achieving this in MVVM/MvvmCross in WPF?
I had some experience to implement events by MVVM maybe it can help you.
As I know there is a package on Microsoft.Xaml.Behaviors.Wpf that will help to get the events and execute a command
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding ClosingWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
and if you want to execute a command in other view models you can use a static command
xmlns:local="clr-namespace:App.Views.Windows"
xmlns:i="http://schemas.microsoft.com/xaml/behaviors"
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{x:static local:ViewModel.ClosingWindow}"/>
</i:EventTrigger>
</i:Interaction.Triggers>

Handling window closing in WPF - MVVM Light

I'm trying to handle the window closing using a solution similar to this, but the handler in my ViewModel is firing on application start, and not when closing.
XAML:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Closing">
<command:EventToCommand Command="{Binding WindowClosing}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
ViewModel:
public RelayCommand<System.ComponentModel.CancelEventArgs> WindowClosing
{
get
{
return new RelayCommand<System.ComponentModel.CancelEventArgs>((args) => {});
}
}
The binding is obviously functioning, but it's just firing at exactly the wrong time. I thought the EventName="Closing" is what was supposed to bind to the actual closing event, but it doesn't matter what's contained there. It always fires on loading. What exactly is supposed to link this to the actual window closing event?

Fire Command or Trigger from DatePicker

as mentioned in the title, I would like to create a trigger on a datepicker, or something equivalent. Here is the situation:
<DatePicker SelectedDate="{BindingDateDebut}"/>
<DatePicker SelectedDate="{Binding DateEnd}"/>
No worries about the binding of dates. Now, I'd like when I change my date, it triggers a Command:
LoadMatrice = new RelayCommand(async () =>
{
await GetParametresMatrice();
});
To access the "GetParametresMatrice()" method for example.
I could add the method in the "DateStart" and "DateEnd" set of course, but sometimes both dates change at the same time (during initialization for example) and without "await", my tasks are done at the same time and the output the code gives bad results.
I try with something like :
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding LoadMatrice}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
But it doesn't fire. Any tips ?
Thank you in advance!
The DatePicker has no SelectionChanged event but it has a SelectedDateChanged event that you can try to handle:
<i:EventTrigger EventName="SelectedDateChanged">

Mvvm light EventToCommand - Setting the Handled property?

I have a tabbed GUI with each tab containing a Frame. I use the EventToCommand with the SelectionChangedEvent whenever a new TabItem is selected. I do this to update the state of the app. This all works fine - a little bit too fine, the event gets fired too often. Here is my problem:
How can I prevent the event from bubbling up the visual tree by setting the Handled property on the event when using the mvvm light eventToCommand feature?
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<mvvm:EventToCommand
Command="{Binding YourCommand}"
PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
Your command needs to provide parameter from method like RelayCommand . This parameter is event args providing Handled property. More here.

How can I trigger an event when the left mouse button gets released in WPF?

I need to trigger an event when the left mouse button gets released. I've tried this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseClick" >
<i:InvokeCommandAction Command="{Binding OnBarGroupChangeCommand}" CommandParameter="{Binding ElementName=ReportsBarGroup, Path=Key}" />
</i:EventTrigger>
</i:Interaction.Triggers>
and this
<igWPF:OutlookBarGroup.InputBindings>
<MouseBinding MouseAction="LeftClick"
Command="{Binding OnBarGroupChangeCommand}" CommandParameter="{Binding ElementName=ReportsBarGroup, Path=Key}"/>
</igWPF:OutlookBarGroup.InputBindings>
These both work. The problem with both cases is that the event fires when the button gets pressed. I need it to fire only when the button gets released. The MouseBinding does not seem to support this. Is there a way to do this with Interaction? What is the best way to handle this? Thanks.
Try EventTrigger event name "MouseLeftButtonUp".
I'm not too familiar with C# but, as far as I'm aware, MouseBinding doesn't allow the support of mouse-up actions, only mouse-down. Take a look at the answer over here
Why don't your try this:
private void btn_MouseUp(object sender, MouseEventArgs e)
{
/////WHAT YOU WANT THE BUTTON TO DO/////
}
If you need to know more about mouse events enter HERE

Categories

Resources