<Button Height="40" Width="40" x:Name="btnup" Command="{Binding UpCommand}" CommandParameter="{Binding ElementName=dgEntities,Path=SelectedItem}" Click="Btnup_OnClick">
这段代码存在的问题是,在OnClick之后才执行Command命令。有没有可能先执行Command命令再执行OnClick操作呢?请帮忙解决。
Execute
之前会评估OnClick
。根据单击处理程序执行的操作,您可以改为在Execute
中调用它,或者向视图模型引发事件,然后再向视图引发事件,最终执行该代码的操作。Code-Behind:
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();
}
}
}