Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button Click and Command Precedence

Tags:

mvvm

wpf

prism

<Button Height="40" Width="40" x:Name="btnup" Command="{Binding UpCommand}" CommandParameter="{Binding ElementName=dgEntities,Path=SelectedItem}" Click="Btnup_OnClick">

The thing thats happening with this code is, Command is being executed after OnClick. Is it possible to execute Command first and then OnClick. Please help....

like image 743
nikhil Avatar asked Nov 18 '14 00:11

nikhil


1 Answers

No, WPF evaluates OnClick before invoking Execute on a bound command.

Depending on what the click handler does; you could invoke it from Execute instead, or raise an event back to the view model, which then raises an event back to the view, which then executes the code.

Something like:

Code-Behind:

  public SomeViewClass
  {
     public SomeViewClass()
     {
         InitializeComponent();

         SomeViewModel viewModel = new SomeViewModel;
         DataContext = viewModel;
         viewModel.SomeCommandCompleted += MoveUp;
     }

     private void MoveUp()
     {
         ...
     }
  }

View Model

public class SomeViewModel
{
    public event Action SomeCommandCompleted;
    public ICommand SomeCommand {get; private set;}

    public SomeViewModel()
    {
        SomeCommand = new DelegateCommand((o) => 
        {
            ...
            if (SomeCommandCompleted != null)
                SomeCommandCompleted();
        }
    }
}
like image 68
BradleyDotNET Avatar answered Nov 07 '22 23:11

BradleyDotNET