当绑定命令时,IsEnabled是否必选?

7

I noticed that the following code:

<Button Content="_Timbres..." 
    Command="{Binding Path=ShowTimbresCommand}" 
    IsEnabled="{Binding Path=CanExecuteShowTimbresCommand}"/>

表现相同的行为:

<Button Content="_Timbres..." 
    Command="{Binding Path=ShowTimbresCommand}">

这意味着CanExecuteShowTimbresCommand自动绑定到IsEnabled属性。这是真的吗?为什么?
1个回答

15
通常,接受Command的控件会在命令的CanExecutefalse时将IsEnabled设置为false,就这样而已。 MSDN
命令的另一个目的是指示操作是否可用。[...] 按钮可以订阅CanExecuteChanged事件,并在CanExecute返回false时禁用或在CanExecute返回true时启用。

1
@MichelKeijzers:如果你迄今为止一直自己处理IsEnabled,那确实是这种情况。 - H.B.
是的,我目前在每个地方都有一个命令绑定和IsEnabled = CanExecute为同一个命令。我假设你在答案中做的额外编辑是自动完成的。 - Michel Keijzers
1
@MichelKeijzers:是的,当您设置命令属性时,应该在内部发生(我还没有看到不遵循此规则的.NET控件)。 - H.B.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接