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?
Related
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>
I want a solution about close popup control when outside scrollviewer mousewheel changed.
<ScrollViewer>
....
<Grid>
<TextBox x:Name="PART_Text"/>
<Popup IsOpen="{Binding IsDropDown}" StayOpen="False"
PlacementTarget{BInding ElementName=PART_Text">
<Border>...</Border>
</Popup>
</Grid>
</ScrollViewer>
I want to make the pop-up window close Automatically, when a wheel moves, not a mouse click
Tip for the future: you'll have better luck getting questions answered if you make it easy for people. Your code doesn't compile on account of 1) your StaysOpen property is misspelled, 2) your PlacementTarget setting doesn't have an assignment operator and opening quotation, 3) your Binding keyword is mis-capitalized and 4) the setter doesn't have a closing bracket.
To answer your question, all you need to do is add a command handler for the PreviewMouseWheel event. Exactly where you intercept the event depends on what behavior you want; if you want it to occur when any control in your application has focus then add it to the MainWindow, otherwise add it to your ScrollViewer:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
<ScrollViewer>
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseWheel">
<i:InvokeCommandAction Command="{Binding PreviewMouseWheelCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Then add a command handler for it back in your view model:
private ICommand _PreviewMouseWheelCommand;
public ICommand PreviewMouseWheelCommand => this._PreviewMouseWheelCommand ?? (this._PreviewMouseWheelCommand = new RelayCommand(OnPreviewMouseWheel));
private void OnPreviewMouseWheel()
{
this.IsDropDown = false;
}
So long as your IsDropDown property supports INPC the popup will disappear whenever a PreviewMouseWheel event occurs.
I'm quit looking for a while and can't find a solution for this supposed easy task. I am using two events via event triggers, the 'Activated' and the 'Closed' event.
I'm using the 'Activated' event to update my Window, but I don't want it be fired when I close the application from the taskbar. Any ideas?
My program contains the following eventtrigger in the XAML-Code:
<i:Interaction.Triggers>
<i:EventTrigger EventName="Activated" >
<i:InvokeCommandAction Command="{Binding WindowActivated}" />
</i:EventTrigger>
<i:EventTrigger EventName="Closing">
<i:InvokeCommandAction Command="{Binding ClosingCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
To my knowledge this isn't possible. And it's got nothing to do with WPF or MVVM, it's how Windows handles this itself. When you right-click on the taskbar you force the focus away from your application, so you'll get a deactivate event. Selecting close from the pop-up menu causes windows to send a WM_SYSCOMMAND/SC_CLOSE but it has to activate your window first, and there's no way at the application level to know the future intention of the system thread that's initiating this action.
As I see it you have 2 choices. The first is to use a DispatcherTimer to add a small delay between the Activate and the update of your window, long enough for the subsequent close message to arrive.
The second is to do your update in a CompositionTarget.Rendering event handler. This event is triggered even when the application doesn't have focus (seomthing you may want to track yourself if you don't want background updates), but the SC_CLOSE event that follows the WM_ACTIVATE event should occur fast enough for it to arrive before the next CompositionTarget.Rendering event, at which point you know your application is closing and can therefore ignore all subsequent updates.
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.
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