nikhil
nikhil

Reputation: 1736

Button Click and Command Precedence

<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....

Upvotes: 6

Views: 4566

Answers (1)

BradleyDotNET
BradleyDotNET

Reputation: 61349

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

Upvotes: 10

Related Questions