Benutzer:MovGP0/WPF/ICommandSource

aus Wikipedia, der freien Enzyklopädie
< Benutzer:MovGP0‎ | WPF
Dies ist die aktuelle Version dieser Seite, zuletzt bearbeitet am 28. Dezember 2015 um 12:49 Uhr durch imported>MovGP0(77247) (→‎ICommandSource).
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


ICommandSource

public sealed class MyControl : Control, ICommandSource
{
    public MyControl()
    {
        InitializeComponent();
    }

    private EventHandler CanExecuteChangedHandler { get; set; }

    #region Command
    private static readonly DependencyProperty CommandProperty = 
        DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(MyControl), 
            new PorpertyMetadata(null, OnCommandChanged));

    public ICommand Command
    {
        get { return (ICommand)GetValue(CommandProperty); }
        set { SetValue(CommandProperty, value); }
    }

    private static void OnCommandChanged(DependencyObject d, DependencyPropertyChangedEventArgs args)
    {
        var control = d as MyControl;
        if(control != null)
            control.OnCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue);
    }

    private void OnCommandChanged(ICommand oldCommand, ICommand newCommand)
    {
        if(oldCommand != null)
            UnhookCommand(oldCommand);
        
        HookupCommand(newCommand);
        CanExecuteChanged(null, null);
    }

    private void UnhookCommand(ICommand oldCommand)
    {
        oldCommand.CanExecuteChanged -= CanExecuteChanged;
    }

    private void HookupCommand(ICommand newCommand)
    {
        CanExecuteChangedHandler = new EventHandler(CanExecuteChanged);

        if(newCommand != null)
            newCommand.CanExecuteChanged += CanExecuteChangedHandler;
    }

    private void CanExecuteChanged(object sender, EventArgs args)
    {
        if(Command == null) return; 
        
        // RoutedCommand
        var routedCommand = Command as RoutedCommand;
        if(routedCommand != null)
        {
            IsEnabled = routedCommand.CanExecute(CommandParameter, CommandTarget);
            return;
        }
        
        // ICommand
        IsEnabled = Command.CanExecute(CommandParameter);
    }
    #endregion

    #region CommandParameter
    public static readonly DependencyProperty CommandParameterProperty = 
        DependencyProperty.Register(nameof(CommandParameter), typeof(object), typeof(MyControl), new PropertyMetadata(null));

    public object CommandParameter
    {
        get { return GetValue(CommandParameterProperty); }
        set { SetValue(CommandParameterProperty, value); }
    }
    #endregion

    #region CommandTarget
    public static readonly DependencyProperty CommandTargetProperty = 
        DependencyProperty.Register(nameof(CommandTarget), typeof(IInputElement), typeof(MyControl), new PropertyMetadata(null));

    public IInputElement CommandTarget
    {
        get { return (IInputElement)GetValue(CommandTargetProperty); }
        set { SetValue(CommandTargetProperty, value); }
    }
    #endregion
}