Pergunta

Eu tenho um problema com o meu programa WPF. Eu estou tentando criar um objeto que irá adicionar manipuladores para todos os controles no mesmo escopo.

A linha a seguir não funciona. O evento não é tratado.

element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true);

Eu também tenho um comando binded ao botão. Assim, a idéia é que eu quero CanExecute do Comando para executar: Isso funciona bem. Eu também quero um manipulador para o PreviewCanExecute:. Isso não funciona

Eu sinto muito que eu não posso explicar melhor.

Ver o meu código abaixo:

XAML:

<Window.Resources>
    <my:PermissionScope x:Key="permissionManager"/>
</Window.Resources>
<StackPanel>
    <TextBox Height="23" Name="textBox1" Width="120" />
    <Button Content="Permission Required" Command="{Binding Path=PermissionRequired}" my:PermissionScope.SharedPermissionScope="{StaticResource permissionManager}"/>
    <Button Content="Permission not required" Command="{Binding Path=PermissionRequired}"/>
</StackPanel>

PermissionScope.cs

public class PermissionScope
{

    public static readonly DependencyProperty SharedPermissionScopeProperty =
        DependencyProperty.RegisterAttached("SharedPermissionScope", typeof(PermissionScope), typeof(PermissionScope),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.Inherits,
        new PropertyChangedCallback(OnUseGlobalSharedPermissionScopeChanged)));

    public static void SetSharedPermissionScope(DependencyObject depObj, PermissionScope scope)
    {
        // never place logic in here, because these methods are not called when things are done in XAML
        depObj.SetValue(SharedPermissionScopeProperty, scope);
    }

    public static PermissionScope GetSharedPermissionScope(DependencyObject depObj)
    {
        // never place logic in here, because these methods are not called when things are done in XAML
        return depObj.GetValue(SharedPermissionScopeProperty) as PermissionScope;
    }

    private static void OnUseGlobalSharedPermissionScopeChanged(DependencyObject depObj, DependencyPropertyChangedEventArgs args)
    {
        if (depObj is Button)
        {
            if (args.OldValue != null)
            {
                RemoveEventHandlers(depObj as UIElement, args.OldValue as PermissionScope);
            }
            if (args.NewValue != null)
            {
                AttachEventHandlers(depObj as UIElement, args.NewValue as PermissionScope);
            }
        }
    }

    private static void AttachEventHandlers(UIElement element, PermissionScope scope)
    {
        if (element != null && scope != null)
        {
            element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler), true); // we need to see all events to subvert the built-in undo/redo tracking in the text boxes
        }
    }

    private static void RemoveEventHandlers(UIElement element, PermissionScope scope)
    {
        if (element != null && scope != null)
        {
            element.AddHandler(CommandManager.PreviewCanExecuteEvent, new CanExecuteRoutedEventHandler(scope.CanExecutedHandler));
        }
    }

    private void CanExecutedHandler(object sender, CanExecuteRoutedEventArgs e)
    {
        if (e.Command is CommandBase)
        {
            bool hasPermission = false;
            hasPermission = ((CommandBase)e.Command).HasPermission();

            ShowControl((UIElement)e.OriginalSource, hasPermission);
        }
    }

    public static void ShowControl(UIElement element, bool show)
    {
        element.Visibility = show ? Visibility.Visible : Visibility.Collapsed;
    }
}

Eu realmente não ajuda.

Com os melhores cumprimentos, Michael

Foi útil?

Solução

Eu descobri o meu eu. Isso só vai trabalhar com RoutedCommands e não ICommand

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top