RoutedCommand.CanExecute(Object, IInputElement) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
确定这 RoutedCommand 是否可以在其当前状态下执行。
public:
bool CanExecute(System::Object ^ parameter, System::Windows::IInputElement ^ target);
[System.Security.SecurityCritical]
public bool CanExecute(object parameter, System.Windows.IInputElement target);
public bool CanExecute(object parameter, System.Windows.IInputElement target);
[<System.Security.SecurityCritical>]
member this.CanExecute : obj * System.Windows.IInputElement -> bool
member this.CanExecute : obj * System.Windows.IInputElement -> bool
Public Function CanExecute (parameter As Object, target As IInputElement) As Boolean
参数
- parameter
- Object
用户定义的数据类型。
- target
- IInputElement
命令目标。
返回
true 如果命令可以在当前命令目标上执行,则为否则,为 false.
- 属性
例外
target 不是 UIElement 或 ContentElement。
示例
下面的示例是 CanExecuteChanged 自定义实现中的 ICommandSource事件处理程序。
this.Command在此示例中为Command属性。ICommandSource 如果该命令不是 null,该命令将强制转换为 a RoutedCommand。 如果命令是一个 RoutedCommand,则 CanExecute 调用该方法传递 CommandTarget 和传递 CommandParameter。 如果命令不是命令 RoutedCommand,则将其强制转换为方法 ICommand ,并 CanExecute 调用该方法传递 CommandParameter。
CanExecute如果该方法返回true,则启用控件;否则,控件处于禁用状态。
private void CanExecuteChanged(object sender, EventArgs e)
{
if (this.Command != null)
{
RoutedCommand command = this.Command as RoutedCommand;
// If a RoutedCommand.
if (command != null)
{
if (command.CanExecute(CommandParameter, CommandTarget))
{
this.IsEnabled = true;
}
else
{
this.IsEnabled = false;
}
}
// If a not RoutedCommand.
else
{
if (Command.CanExecute(CommandParameter))
{
this.IsEnabled = true;
}
else
{
this.IsEnabled = false;
}
}
}
}
Private Sub CanExecuteChanged(ByVal sender As Object, ByVal e As EventArgs)
If Me.Command IsNot Nothing Then
Dim command As RoutedCommand = TryCast(Me.Command, RoutedCommand)
' If a RoutedCommand.
If command IsNot Nothing Then
If command.CanExecute(CommandParameter, CommandTarget) Then
Me.IsEnabled = True
Else
Me.IsEnabled = False
End If
' If a not RoutedCommand.
Else
If Me.Command.CanExecute(CommandParameter) Then
Me.IsEnabled = True
Else
Me.IsEnabled = False
End If
End If
End If
End Sub
注解
确定是否可以
在 <