RoutedCommand.CanExecute(Object, IInputElement) Método
Definição
Importante
Algumas informações se referem a produtos de pré-lançamento que podem ser substancialmente modificados antes do lançamento. A Microsoft não oferece garantias, expressas ou implícitas, das informações aqui fornecidas.
Determina se isso RoutedCommand pode ser executado em seu estado atual.
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
Parâmetros
- parameter
- Object
Um tipo de dados definido pelo usuário.
- target
- IInputElement
O destino do comando.
Retornos
true se o comando puder ser executado no destino de comando atual; caso contrário, false.
- Atributos
Exceções
target não é um UIElement ou ContentElement.
Exemplos
O exemplo a seguir é um CanExecuteChanged manipulador de eventos de uma implementação personalizada de ICommandSource.
this.Command neste exemplo está a Command propriedade no ICommandSource. Se o comando não nullestiver, o comando será convertido em um RoutedCommand. Se o comando for umRoutedCommand, o CanExecute método será chamado passando o CommandTarget .CommandParameter Se o comando não for um RoutedCommand, ele será convertido em um ICommand e o CanExecute método será chamado passando o CommandParameter.
Se o CanExecute método retornar true, o controle será habilitado; caso contrário, o controle será desabilitado.
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
Comentários
A lógica real que determina se uma RoutedCommand pode ser executada no destino de comando atual não está contida nos CanExecute métodos, em vez disso CanExecute gera os PreviewCanExecuteCanExecute eventos que túnel e bolha através da árvore de elementos procurando um objeto com um CommandBinding. Se um CommandBinding para isso RoutedCommand for encontrado, o CanExecuteRoutedEventHandler anexado CommandBinding será chamado. Esses manipuladores fornecem a lógica de programação para determinar se ele RoutedCommand pode ser executado ou não.
O PreviewCanExecute e PreviewExecuted os eventos são gerados na CommandTarget. Se não CommandTarget estiver definido no ICommandSource, os eventos e os CanExecutePreviewCanExecute eventos serão gerados no elemento com foco no teclado.