<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....
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();
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With