[英]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.