Please see this EventTrigger:
<ListView Name="ListViewFiles">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseLeftButtonDown">
<i:InvokeCommandAction Command="{Binding ListViewItemMouseLeftButtonDownCommand}"
CommandParameter="{Binding ElementName=ListViewFiles, Path=SelectedItem}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListView>
So i want to achieve 2 things:
I try to use MouseLeftButtonDown instead of PreviewMouseLeftButtonDown but the event is not fired (why ??).
When the user click and hold the left button i want the event the fire after 500 milliseconds currently i am using timer - any other approach ?
MouseLeftButtonDown probably isn't activating your trigger because of how WPF handles RoutedEvents. When a RoutedEvent gets marked "handled", WPF doesn't call any further event handlers for it.
ListView is almost definitely handling MouseLeftButtonDown internally, probably to change the selected item. Because MouseLeftButtonDown is a bubbling event, that means it gets marked as handled inside the ListView before it gets up the visual tree to your code, so it never arrives.
PreviewMouseLeftButtonDown, on the other hand, is a tunneling event, so it reaches your code before getting into the ListView.
As for the second part of your question, I don't know any better way to trigger code after some time than by using some sort of timer. Also, if you want to trigger it only after they've been holding the button for that amount of time, make sure you handle the PreviewLeftMouseButtonUp event to turn the timer off if they release the button before the time is up.
Related
I am using mvvm pattern and using integerupdown button in the WPF application.
To invoke command currently I am using ValueChanged trigger as following -
<i:Interaction.Triggers>
<i:EventTrigger EventName="ValueChanged">
<i:InvokeCommandAction Command="{Binding PercentChangedCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
This works perfectly fine when user changes value using mouse click on up or down arrow.
Problem is the moment user wants to type in textbox, the event get triggered for every character typed in. Instead I want to allow user to type in numbers and trigger command only on hitting Enter.
Please suggest.
if ValueChanged is triggered on every KeyStroke then your Bindings UpdateSourceTrigger is likely set to default (ValueChanged). Start by setting it to LostFocus. Additionally catch the KeyDown-event with an EventTrigger and check if the key pressed was the Enter-Key.
If its the enter-key then simply call your command.
Edit: if you need a reusable solution for Commit-On-Enter-scenarios have a look at this question.
Found a property - UpdateValueOnEnterKey="True". This will not invoke value changed on every character change, but only on Enterkey press
Below is my xaml:
<i:EventTrigger EventName="Thumb.DragDelta" >
<ei:CallMethodAction MethodName="AudioTimeSliderDragDelta" TargetObject="{Binding}"/>
</i:EventTrigger>
<i:EventTrigger EventName="Thumb.DragCompleted">
<ei:CallMethodAction MethodName="AudioTimeSliderDragCompleted" TargetObject="{Binding}"/>
</i:EventTrigger>
Although i am able to invoke slider MouseMove event, Somehow the dragEvent handler events doesn't get fired, hence the related methods never gets executed.
I have tried DragEnter as well. Dint work.
Can someone please help me?
You can set the Binding of the slider value to be triggered by PropertyChanged so you will get an update for every value change. So if the user dragged the slider from 1 to 100 you will get 100 updates which will be the behavior you were after.
I'm attempting to use a Command, defined in my view model with EventTriggers as defined in xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
I have changed the control from a Microsoft.Surface.Presentation.Controls.SurfaceSlider-derived control to a regular WPF Button. With the Button, the same ConfirmOrderCommand gets fired if I use the EventName="MouseEnter", but if I use EventName="Click" nothing happens. The XAML for the Button is here.
<Button Name="ConfirmButton"
IsEnabled="{Binding IsEnabled}"
Style="{StaticResource ConfirmButtonStyle}"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding ConfirmOrderCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
I want the user to be able to click the button to fire the command. Can I get the Click event to work for a Button, or do I need to look for another event? I've also failed to get MouseLeftButtonUp to work.
Try EventName="Button.Click". Anyways, you can just set the Command Property of the Button itself, and remove the Interaction.Triggers part.
The actual reason the Button.Click would not work, was determined after much pain and finally asking someone on the project after he woke up in the morning.
Our application was stealing almost all of the Button events with FrameworkElement Preview delegates.
public static void RegisterEvents(FrameworkElement parent)
{
parent.PreviewMouseDown += MouseDown;
parent.PreviewMouseUp += MouseUp;
// even more of these
}
The delegates would then use System.Window.Input.TouchDevice to ReportDown() in the case of MouseDown etc. All this time I just thought I didn't know how the standard WPF button usually works. I guess the moral of this story is events don't have to start at the child control and then propagate up to their containers until Handled = true. Preview allows the container to capture the events first.
I have a couple of Storyboards in my view that I would like to trigger from the ViewModel if possible. Is there a simple way or elegant way of doing this. Here is what I am trying to do.
Person Clicks on a Button-->RelayCommand (In the ViewModel), the Relay Command should then play the storyboard. Also one more thing, I would like to also trigger the storyboard animation by itself in the ViewModel without any interaction.
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonDown">
<cmd:EventToCommand Command="{Binding ButtonPress}" CommandParameterValue="RedButtonLight">
</cmd:EventToCommand>
</i:EventTrigger>
</i:Interaction.Triggers>
I know it's a long time ago. But I've written a detailed blog post about Triggering Storyboards and MVVM.
http://mark.mymonster.nl/2010/12/14/trigger-a-storyboard-on-viewmodel-changes/
If the button click is purely to power a view-related thing and isn't doing any actual application logic, then I would argue that you can do all this in the code-behind of the view class.
If this isn't the case then I would use a property on the Presentation (ViewModel) to signal that the Presentation is in a state, and have the view react to the PropertyChanged event and start the storyboard. This is assuming you are implimenting INotifyPropertyChanged on your Presentation class.
Have a look at the expression samples. There is a trigger for events from the datacontext. DataEventTrigger
You could use that to trigger a ControlStoryboardAction to start the story board whenever your viewmodel raises a particular event.
Your viewmodel could then raise the event as part of the command as well as at other times.
Heres how you can do it in blend without touching a line of xaml or code :
http://www.basarat.com/2011/05/expression-blend-starting-storyboard.html
I'm just starting with MVVM light, but so far it allowed me to solve some of my issues. Infortunately I'm struggling with relatively sime issues in Silverlight.
Let's assume the following button with EventToCommand:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding UpdateAccountsCommand, Mode=OneWay}" CommandParameter="{Binding SelectedIndex, ElementName=lstLedgers}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</Button>
How to assign this code to SelectedIndexChanged event?
Futher issue - how to assign a command in C# code? The problem is as follows: I'm developing Windows Phone 7 app. Application Bar needs to be initiated in C# (as far as I know there is no xaml code for application bar at this stage). As a result I have no idea how to bind a command to a application bat button in from c#, now xaml.
Thanks in advance your your help.
How to assign this code to SelectedIndexChanged event
The event name is in the trigger's initial xaml line. The EventName is what triggers something to happen. You'll need to make sure that you have this trigger on an object with a SelectedIndexChanged event. Button doesn't have that event.
...
<i:EventTrigger EventName="SelectedIndexChanged">
...
If you are breaking mvvm for the application bar, I don't know why you'd want to create a trigger. You might as well go the direct route by accessing the events you want directly. You can do it, don't get me wrong... just this code is a lot cleaner:
ApplicationBarButton.Click += new RoutedEventHandler(OnApplicationBarButton_Click);