RoutedCommand.CanExecute(Object, IInputElement) Metódus
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Meghatározza, hogy ez RoutedCommand végrehajtható-e a jelenlegi állapotában.
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
Paraméterek
- parameter
- Object
Felhasználó által definiált adattípus.
- target
- IInputElement
A parancscél.
Válaszok
trueha a parancs végrehajtható az aktuális parancscélon; egyéb esetben. false
- Attribútumok
Kivételek
target nem a UIElement vagy ContentElement.
Példák
Az alábbi példa egy CanExecuteChanged eseménykezelő, amely egy egyéni implementációból származik ICommandSource.
this.Command ebben a példában a Command tulajdonság a ICommandSource. Ha a parancs nem null, a parancsot a rendszer egy RoutedCommand. Ha a parancs egy RoutedCommand, akkor a CanExecute metódust a rendszer az és a CommandTargetCommandParameterparancs átadásának hívja. Ha a parancs nem egy RoutedCommand, akkor a rendszer egy ICommand parancsra irányítja át, és a CanExecute metódus neve a következő lesz: passing the CommandParameter.
Ha a CanExecute metódus visszatér true, akkor a vezérlő engedélyezve van, ellenkező esetben a vezérlő le van tiltva.
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
Megjegyzések
A tényleges logika, amely meghatározza, hogy az aktuális parancscélon végrehajtható-e, RoutedCommand nem szerepel a CanExecute metódusokban, hanem CanExecute azokat az eseményeket emeli ki, amelyek az PreviewCanExecuteCanExecute elemfán áthaladva egy objektumot keresnek egy CommandBinding. Ha egy CommandBinding ilyen RoutedCommand található, akkor a rendszer meghívja a CanExecuteRoutedEventHandler csatolt elemet CommandBinding . Ezek a kezelők biztosítják a programozási logikát annak meghatározásához, hogy a RoutedCommand rendszer végrehajtható-e vagy sem.
A PreviewCanExecute rendszer PreviewExecuted az eseményeket a CommandTarget. Ha a CommandTarget beállítás nincs beállítva, ICommandSourcea rendszer a PreviewCanExecuteCanExecute billentyűzetfókuszú elemen emeli ki az eseményeket.