Benutzer:MovGP0/WPF/ICommandSource

aus Wikipedia, der freien Enzyklopädie
Zur Navigation springen Zur Suche springen
   MovGP0        Über mich        Hilfen        Artikel        Weblinks        Literatur        Zitate        Notizen        Programmierung        MSCert        Physik      


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
}