views:

255

answers:

1

I'm new to SilverLight and Mvvm-Light. I have a DataForm on my view that displays/edits a SelectedPerson property (a Person object) of my view model.

I want to execute a command on my viewmodel when the user clicks the Save button but don't want to take action if the user clicks cancel.

I added the following to my ViewModel:

public RelayCommand PersonEditEnded {get; set;}
...
public void Initialize()
{
  PersonEditEnded = new RelayCommand(DoSomething);
  ...
}

public void DoSomething()
{
}

I added the following to my View:

<toolkit:DataForm x:Name="PersonForm" ... CurrentItem="{Binding SelectedPerson, Mode=TwoWay}">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="EditEnded">
      <gs:EventToCommand Command="{Binding PersonEditEnded, Mode=OneWay}"/>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</toolkit:DataForm>

This works and the DoSomething method is being called when the user presses Submit. However, DoSomething is also called when user presses Cancel. Is there a way to know which button was pressed or to supress the call when Cancel is pressed?

Thanks for whatever help you can offer!

+1  A: 

Hi,

The information you are looking for is hidden quite deeply into the DataFormEditEndedEventArgs, in the EditAction. This property of type DataFormEditAction can be either Commit or Cancel.

There is a way to pass the argument to the Command: Set the property PassEventArgsToCommand (on the EventToCommand object) to True. On the ViewModel, use a RelayCommand. The parameter of the delegate will be the event args, then you can do something like

e =>
{
    if (e.CommitAction == DataFormEditAction.Commit)
    {
        DoSomething();
    }
}

Hope it helps,

Laurent

LBugnion