按钮单击和命令优先顺序

时间:2014-11-18 00:00:02

标签: wpf mvvm prism

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

这段代码发生的事情是,在OnClick之后执行Command。是否可以先执行Command然后再执行OnClick。请帮帮....

1 个答案:

答案 0 :(得分:6)

不,WPF在绑定命令上调用OnClick之前评估Execute

取决于点击处理程序的功能;你可以从Execute调用它,或者将一个事件引回视图模型,然后将事件引回到视图,然后视图执行代码。

类似的东西:

<强>代码隐藏:

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

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

     private void MoveUp()
     {
         ...
     }
  }

查看模型

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

    public SomeViewModel()
    {
        SomeCommand = new DelegateCommand((o) => 
        {
            ...
            if (SomeCommandCompleted != null)
                SomeCommandCompleted();
        }
    }
}