简体   繁体   English

CommandParameter 绑定在 Command.CanExecute 触发后解析

[英]CommandParameter Binding Resolves after Command.CanExecute Fires

I have a button with a bound command and command parameter:我有一个带有绑定命令和命令参数的按钮:

    <Button Margin="5,0,5,5" Style="{StaticResource MainButton}" Grid.Row="2" Grid.RowSpan="2" Grid.Column="3" Padding="0">
         <Button.CommandParameter>
              <MultiBinding Converter="{StaticResource SwapArgsConverter}" ConverterParameter="-1">
                   <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType=tlq:SmartWarningsWindow}" Path="DataContext.SelectedWarning"></Binding>
                   <Binding Path="Rank"></Binding>
              </MultiBinding>
         </Button.CommandParameter>
         <Button.Command>
              <Binding RelativeSource="{RelativeSource FindAncestor, AncestorType=tlq:SmartWarningsWindow}" Path="DataContext.SwapCommand"></Binding>
         </Button.Command>
     </Button>

The bindings all work fine, but the Command binding resolves, and the ICommand.CanExecute method fires before the CommandParameter binding resolves.绑定都可以正常工作,但Command绑定会解析,并且ICommand.CanExecute方法会在CommandParameter绑定解析之前触发。 This is causing my control to be incorrectly disabled when the window loads.这导致我的控件在窗口加载时被错误禁用。

I have tried several things, including placing the Command Binding after the CommandParameter binding like you see above (originally the command binding was an attribute).我尝试了几件事,包括将Command绑定放在像上面看到的CommandParameter绑定之后(最初命令绑定是一个属性)。

Does anyone know a way to force the CommandParameter binding to resolve first?有谁知道强制首先解决CommandParameter绑定的方法吗?

You might be missing this in your command class:你可能在你的命令类中遗漏了这个:

public event EventHandler CanExecuteChanged
{
    add { CommandManager.RequerySuggested += value; }
    remove { CommandManager.RequerySuggested -= value; }     
}

An event is raised whenever the CommandManager thinks that changes had happened and will affect the ability of commands to execute.每当 CommandManager 认为发生了更改并将影响命令执行的能力时,就会引发一个事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 WPF CommandParameter绑定和canExecute - WPF CommandParameter binding and canExecute WPF MenuItem Command.Execute不触发,但Command.CanExecute可以触发 - WPF MenuItem Command.Executed not firing, but Command.CanExecute does 如何从子控件使用 Command.CanExecute - How to use Command.CanExecute from a child control 为什么我的Command.CanExecute在单元测试中总是返回false? - Why does my Command.CanExecute always return false in unit test? MVVMLight 命令 CanExecute 在异步调用后不触发 - MVVMLight command CanExecute not firing after async call 如果Command绑定解析为null,为什么启用了按钮? - Why is a button enabled if the Command binding resolves to null? 如何使用ReactiveUI从CommandParameter确定“ canExecute”? - How to determine “canExecute” from the CommandParameter with ReactiveUI? 在执行命令2之后通知命令1的CanExecute - Notify CanExecute of command1 after command2 is executed 在代码背后将DependencyProperty绑定到CommandParameter - Binding DependencyProperty in codebehind to CommandParameter 当我将CommandParameter设置为某个Binding时,为什么我的ICommand.CanExecute(object)参数为null,但当我将其设置为某个静态值时为非null? - Why is my ICommand.CanExecute(object) parameter null when I set CommandParameter to some Binding, but non-null when I set it to some static value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM