开发者

How to tell which dataform button ended edit when using EventToCommand

开发者 https://www.devze.com 2022-12-29 14:44 出处:网络
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'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:Inter开发者_运维问答action.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!


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


If you want act on the data you can also make a binding to CurrentItem and use it through your command (or elsewhere) so you can do whatever you want to the item.

0

精彩评论

暂无评论...
验证码 换一张
取 消