This project has moved. For the latest updates, please go here.

Binding Order

Jun 16, 2010 at 8:34 PM


 I need a help to solve an intrinsic problem with the EventToCommand (or the binding in general). Suppose I have a MainView and a MainViewModel. The MainView has a ListBox like this one:  

<ListBox x:Name="lst" ItemsSource="{Binding Persons}" SelectedItem="{Binding SelectedPerson,Mode=TwoWay}" >
   <i:Interaction.Triggers >
      <i:EventTrigger EventName="SelectionChanged">
         <cmd:EventToCommand Command="{Binding SelectPersonCommand}" CommandParameter="{Binding SelectedItem,ElementName=lst}"/>
      </i:EventTrigger > 
   </i:Interaction.Triggers >

If I set SelectedPerson = null by code (because i want to clear the selection in the listbox) the trigger fires but the CommandParameter is not updated yet and command receive the old value.  I can see that the CommandParameter get updated after the command is executed. The same happen if I bind the CommandParameter directly to the SelectedPerson property of the ViewModel

In this simple case I could workaround the problem by not passing the value through the command parameter and getting the value in the viewmodel code, but I have a dynamic number of listbox binded to the same command and the CommandParameter become crucial.

Is there a way to control the order in which Triggers and Bindings are executed ?


Jun 16, 2010 at 9:24 PM


Typically in this case you would trigger whatever calculation you are doing in the SelectedPerson property. Use something like this:

public const string SelectedPersonPropertyName = "SelectedPerson";
private Person _person = null;
public Person SelectedPerson
        return _person;

        if (_person == value)

        _person = value;

To answer your question, I am not aware of a way to control the order in which the events, triggers and bindings are fired.



Jun 17, 2010 at 9:25 AM

Thank You for the reply.

You right, it is much better to handle the SelectionChanged directly in the setter of the property binded to the  SelectedItem, and it does solve my problem.

Thank You