Silverlight. The order of triggers invocation - silverlight

There is a Silverlight UserControl which got an MouseLeftButtonUp event. In xaml I add two triggers for this event. In which order Silverlight xaml-parser will parse and attach those triggers and can I be sure that trigger above will invoke first?
...
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<i:InvokeCommandAction Command="{StaticResource someCommand}"/>
<AttachedBehaviors:SomeBehavior Parameter="Apple"/>
</i:EventTrigger>
</i:Interaction.Triggers>
...
UPDATE: Found connected issue In WPF, does the order of Triggers matter? which says:
WPF is processing triggers in declared order.
Hope, Silverlight behave exactly that same.

Even if the order is defined I wouldn't do this. Add the order you want to the handler of the command.
void someCommand_Executed()
{
DoFirstThing();
DoSecondThing();
}
This kind of code will cause pain in the long run because the order is not easily enforced and you are creating a dependency between two methods.

Related

What ways are there for handling a double click event without using codebehind?

I saw an article detailing how to do this using attached behaviors which I like a lot. However, the download doesn't work and the article is pretty old (a couple of years).
What is the current way for handling events like this without using codebehind? Are there any that are MVVM-specific?
You can use the expression blend extensions:
<ListBox>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick">
<i:InvokeCommandAction Command="CommandNameHere"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>
It is another dependency, but it does seem to work well in .NET 4.
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10801
Edit: You can also use the CommandParameter To Pass In Extra Things (Like The Selected Item View Model)

How can I bind an Event to changing the property on another element in the tree in XAML WPF?

I'll start off with the code as it should be fairly self-explanatory:
<commonControls:SearchTextBox
x:Name="searchTextBox"
Margin="6,0"
HorizontalAlignment="Right"
MinWidth="50"
Width="130"
SearchMode="Instant"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Search">
<cmd:EventToCommand Command="{Binding Search}"
CommandParameter="{Binding ElementName=searchTextBox, Path=Text}" />
</i:EventTrigger>
<i:EventTrigger EventName="Cancel">
<!-- Code to set searchTextBox.Text to Empty -->
</i:EventTrigger>
</i:Interaction.Triggers>
</commonControls:SearchTextBox>
The SearchTextBox has an Event called Cancel which executes if the user clicks the X button on the right hand side of the TextBox. What I would like to do is hook onto that event, and clear the Text property of the TextBox. I'm looking for a way to do this purely in XAML, with no Code Behind, and without hitting the ViewModel.
You can achieve this using a concept called 'attached behaviours', attached properties that on attachment handle events on the target object and perform some action as a result. See the following article:
http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx
I see you are already using blend interactions. Blend provides a simple framework for behaviours making them easier to implement, you simple override the OnAttach method to add your logic. See the example here:
http://www.scottlogic.co.uk/blog/colin/2011/06/metro-in-motion-part-7-panorama-prettiness-and-opacity/

MVVM- Trigger Storyboard in the View Model in Silverlight

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

Conditional execution of EventTriggers in Silverlight 3

I'm currently working on the UI of a Silverlight application and need to be able to change the visual state of a control to one of two possible states based on it's current state when handling the same event trigger.
For example:
I have a control that sits partially in a clipping path, when I click the visible part of the control I want to change the state to "visible" and if I click it again when it is in its "visible" state I want to change to the "hidden" state.
Example XAML:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<ic:GoToStateAction StateName="Visible"/>
<ic:GoToStateAction StateName="Hidden"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Where "i" is "System.Windows.Interactivity;assembly=System.Windows.Interactivity" and "ic" is "Microsoft.Expression.Interactivity.Core;assembly=Microsoft.Expression.Interactions". I'm currently working in Expression Blend 3 and would prefer to have a XAML only solution but am not opposed to coding this if it is completely necessary. I have tried recording a change in the target state name in Blend but this did not work.
Any thoughts on this?
If you have only 2 states the easiest would be to simply call GoToNextState to rotate between states. E.g.:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<si:GoToNextState/>
</i:EventTrigger>
</i:Interaction.Triggers>
If you have other states as well, then:
add a property on the underlying viewmodel IsVisible
have a trigger that would call a method (using CallMethod action) that would toggle that property om MouseLeftButtonUp
have a DataStateBehavior bound to IsVisible property
E.g. something like this:
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseLeftButtonUp">
<si:CallDataMethod Method='ToggleIsVisible'/>
</i:EventTrigger>
</i:Interaction.Triggers>
<i:Interaction.Behaviors>
<si:DataStateBehavior Binding='{Binding IsVisible}' Value='True' TrueState='Visible' FalseState='Hidden'/>
</i:Interaction.Behaviors>
In the end I achieved this by creating a simple custom action called ToggleStateAction to encapsulate this behaviour for me.

How to assign RelayCommand to Click or SelectedIndexChanges events?

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);

Resources