簡體   English   中英

使用依賴屬性的 WPF 按鈕快捷鍵

[英]WPF Button shortcut key using Dependency Property

我正在使用一組標准按鈕,在 WPF 應用程序中重復使用,並希望為每個按鈕添加一個快捷鍵。

所以我有一個 ControlTemplate 包含與 Command 綁定到標准命令的按鈕

我正在嘗試通過將依賴屬性添加到按鈕並將父樹向上導航到最近的用戶控件,將 KeyBinding 添加到包含按鈕的用戶控件。

我可以從模板按鈕獲取 DP 值以設置鍵和修飾符,但無法獲取命令(可能是因為它直到稍后才綁定??)

任何想法我怎么能:

  1. 按照這種方法從模板中獲取或創建命令
  2. 或者解決后獲取Command,然后設置KeyBinding

PS:我已經在單獨的 DP 中設置了 Key 和 Modifiers,但希望有一個 KeyBinding DP,然后在 XAML 中設置 ShortcutBinding.Key 和 ShortcutBinding.Modifers。 有沒有辦法像這樣在 XAML 中設置 DP 類的屬性?

從按鈕組模板中提取的 XAML:

                <ctrl:ButtonShortcut 
                    x:Name="btnUpdate"
                    Style="{StaticResource EditButtonStyle}"
                    Command="{Binding DataContext.UpdateCommand, RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}"
                    Content="Update"
                    ShortcutKey="U"
                    ShortcutModifiers="Ctrl"/>

DP 類,繼承自 Button,實現 Key 和 Modifiers 以與綁定到 Button 的相同 Command 鏈接:

    public partial class ButtonShortcut : Button
{
    public KeyBinding ShortcutBinding { get; set; }

    public Key ShortcutKey
    {
        get { return (Key)GetValue(ShortcutKeyProperty); }
        set { SetValue(ShortcutKeyProperty, value); }
    }
    public static readonly DependencyProperty ShortcutKeyProperty =
        DependencyProperty.Register("ShortcutKey", typeof(Key), typeof(ButtonShortcut), new PropertyMetadata(Key.None, ShortcutKeyChanged));

    public ModifierKeys ShortcutModifiers
    {
        get { return (ModifierKeys)GetValue(ShortcutModifiersProperty); }
        set { SetValue(ShortcutModifiersProperty, value); }
    }
    public static readonly DependencyProperty ShortcutModifiersProperty =
        DependencyProperty.Register("ShortcutModifiers", typeof(ModifierKeys), typeof(ButtonShortcut), new PropertyMetadata(ModifierKeys.None, ShortcutKeyChanged));

    private static void ShortcutKeyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var btn = d as ButtonShortcut;
        if (btn != null)
        {
            FrameworkElement uc = btn.Parent as FrameworkElement;
            while (uc?.Parent != null && uc is not UserControl)
                uc = uc.Parent as FrameworkElement;

            if (btn.ShortcutBinding == null)
            {
                btn.ShortcutBinding = new KeyBinding();
            }
            var bindings = btn.CommandBindings;
            if (e.NewValue is Key)
                btn.ShortcutBinding.Key = (Key)e.NewValue;
            if (e.NewValue is ModifierKeys)
                btn.ShortcutBinding.Modifiers = (ModifierKeys)e.NewValue;

            //So far, so good, but I cannot find the Command to apply to the KeyBinding
            btn.ShortcutBinding.Command = btn.Command;
            btn.ShortcutBinding.CommandParameter = btn.CommandParameter;
            if (btn.Command == null)
                System.Diagnostics.Debug.Print("not in Commmand");
            if (btn.CommandBindings.Count == 0)
                System.Diagnostics.Debug.Print("not in CommandBindings");
            if (btn.ReadLocalValue(CommandProperty) == DependencyProperty.UnsetValue)
                System.Diagnostics.Debug.Print("not in DP CommandProperty");


            if (btn.ShortcutBinding.Key != Key.None && uc != null)
            {
                if (!uc.InputBindings.Contains(btn.ShortcutBinding))
                    uc.InputBindings.Add(btn.ShortcutBinding);

            }
        }
    }

    public ButtonShortcut()
    {
        InitializeComponent();
    }

}

這看起來像一個非常難聞的設計。 子控件不應配置父控件。 特別是不要添加孩子假裝提供的“功能”:在您的情況下,按鈕無法執行關鍵手勢 - 它只定義它們。
由於父控件是手勢的目標,它必須定義執行手勢所需的處理程序。 這意味着,所有操作和職責都在父UserControl中,而不是在Button (或子元素)中。 類永遠不應該知道其他類的細節。 按鈕永遠不應該知道哪個父級將執行它的命令以及如何首先注冊命令。

按鈕只是一個被動源。 它本身不處理命令或手勢。 因此,它永遠不會注冊CommandBindingInputBinding
通常,您將Button.Command綁定到在命令目標上定義的命令。 該按鈕在目標上調用此命令(或引發RoutedCommand )並且目標執行相應的操作。

在按鈕上定義命令或按鍵手勢是沒有意義的。 該按鈕不會執行它們。 必須在相關職責所在的目標上定義命令、鍵和鼠標手勢。

您可以讓父UserControl (我在此示例中假設它是命令目標)定義一個RoutedCommand 使用以下命令注冊相應的按鍵手勢:

解決方案 1

MyUserControl.xaml.cs

partial class MyUserControl : UserControl
{
  public static RoutedCommand DoActionCommand { get; }

  static MyUserControl()
  {
    var gestures = new InputGestureCollection
    {
      new KeyGesture(Key.U, ModifierKeys.Control),
    };

    DoActionCommand = new RoutedUICommand(
      "Do something", 
      nameof(DoActionCommand), 
      typeof(CommandTargetUserControl),
      gestures);
  }

  public MyUserControl()
  {
    InitializeComponent();

    this.CommandBindings.Add(new CommandBinding(DoActionCommand, ExecuteDoActionCommand));
  }
}

CommandTargetUserControl.xaml

<MyUserControl>
  <Button Command="{x:Static local:CommandTargetUserControl.DoActionCommand}" />
</MyUserControl>

解決方案 2

要允許在不修改源和目標的情況下配置默認鍵手勢,您可以實現附加行為。 此行為基本上將由鍵手勢調用的掛鈎命令委托給使用特定鍵手勢注冊的實際命令源。 命令源和命令目標是完全解耦的。 輸入綁定被顯式配置為尊重封裝(沒有靜默和意外修改)。

使用示例

<Window local:KeyGestureDelegate.IsKeyGestureDelegationEnabled="True">
  <local:KeyGestureDelegate.TargetKeyGestures>
    <InputGestureCollection>
      <KeyGesture>Ctrl+U</KeyGesture>
      <KeyGesture>Ctrl+Shift+M</KeyGesture>
    </InputGestureCollection>
  </local:KeyGestureDelegate.TargetKeyGestures>

  <StackPanel>
    <Button Command="{Binding SomeCommand}" 
            local:KeyGestureDelegate.SourceKeyGesture="Ctrl+U"
            local:KeyGestureDelegate.IsKeyGestureCommandExecutionEnabled="True" />
    <Button Command="{Binding SomeOtherCommand}"
            local:KeyGestureDelegate.SourceKeyGesture="Shift+Ctrl+M"
            local:KeyGestureDelegate.IsKeyGestureCommandExecutionEnabled="True" />
  </StackPanel>
</Window>

實現示例

KeyGestureDelegate.cs
此示例中使用的RelayCommand的實現可以在Microsoft Docs:Relaying Command Logic中找到。

public class KeyGestureDelegate : DependencyObject
{
  // Custom KeyGesture comparer for the Dictionary
  private class KeyGestureComparer : EqualityComparer<KeyGesture>
  {
    public override bool Equals(KeyGesture? x, KeyGesture? y)
      => (x?.Key, x?.Modifiers).Equals((y?.Key, y?.Modifiers));

    public override int GetHashCode([DisallowNull] KeyGesture obj)
      => HashCode.Combine(obj.Key, obj.Modifiers);
  }

  private static ICommand KeyGestureDelegateCommand { get; } = new RelayCommand(ExecuteKeyGestureDelegateCommand);

  public static bool GetIsKeyGestureDelegationEnabled(DependencyObject attachedElement) => (bool)attachedElement.GetValue(IsKeyGestureDelegationEnabledProperty);
  public static void SetIsKeyGestureDelegationEnabled(DependencyObject attachedElement, bool value) => attachedElement.SetValue(IsKeyGestureDelegationEnabledProperty, value);
  public static readonly DependencyProperty IsKeyGestureDelegationEnabledProperty = DependencyProperty.RegisterAttached(
    "IsKeyGestureDelegationEnabled",
    typeof(bool),
    typeof(KeyGestureDelegate),
    new PropertyMetadata(default(bool), OnIsKeyGestureDelegationEnabled));

  public static bool GetIsKeyGestureCommandExecutionEnabled(DependencyObject attachedElement) => (bool)attachedElement.GetValue(IsKeyGestureCommandExecutionEnabledProperty);
  public static void SetIsKeyGestureCommandExecutionEnabled(DependencyObject attachedElement, bool value) => attachedElement.SetValue(IsKeyGestureCommandExecutionEnabledProperty, value);
  public static readonly DependencyProperty IsKeyGestureCommandExecutionEnabledProperty = DependencyProperty.RegisterAttached(
    "IsKeyGestureCommandExecutionEnabled",
    typeof(bool),
    typeof(KeyGestureDelegate),
    new PropertyMetadata(default(bool), OnIsKeyGestureCommandExecutionEnabled));

  public static InputGestureCollection GetTargetKeyGestures(DependencyObject obj) => (InputGestureCollection)obj.GetValue(TargetKeyGesturesProperty);
  public static void SetTargetKeyGestures(DependencyObject obj, InputGestureCollection value) => obj.SetValue(TargetKeyGesturesProperty, value);

  public static readonly DependencyProperty TargetKeyGesturesProperty = DependencyProperty.RegisterAttached(
    "TargetKeyGestures",
    typeof(InputGestureCollection),
    typeof(KeyGestureDelegate),
    new PropertyMetadata(default(InputGestureCollection), OnTargetKeyGesturesChanged));

  public static KeyGesture GetSourceKeyGesture(DependencyObject attachedElement) => (KeyGesture)attachedElement.GetValue(SourceKeyGestureProperty);
  public static void SetSourceKeyGesture(DependencyObject attachedElement, KeyGesture value) => attachedElement.SetValue(SourceKeyGestureProperty, value);
  public static readonly DependencyProperty SourceKeyGestureProperty = DependencyProperty.RegisterAttached(
    "SourceKeyGesture",
    typeof(KeyGesture),
    typeof(KeyGestureDelegate),
    new PropertyMetadata(default(KeyGesture), OnSourceKeyGestureChanged));

  // Remember added InputBindings to enable later removal
  private static Dictionary<UIElement, IList<InputBinding>> InputBindingTargetMap { get; } = new Dictionary<UIElement, IList<InputBinding>>();

  // Lookup command sources that map to a particular gesture
  private static Dictionary<KeyGesture, IList<ICommandSource>> InputBindingSourceMap { get; } = new Dictionary<KeyGesture, IList<ICommandSource>>(new KeyGestureComparer());

  private static void OnIsKeyGestureDelegationEnabled(DependencyObject attachedElement, DependencyPropertyChangedEventArgs e)
  {
    if (attachedElement is not UIElement keyGestureHandler)
    {
      throw new ArgumentException($"Attached element must be of type {typeof(UIElement)}.");
    }

    InputGestureCollection gestures = GetTargetKeyGestures(keyGestureHandler);
    if ((bool)e.NewValue)
    {
      RegisterKeyBinding(keyGestureHandler, gestures);
    }
    else
    {
      UnregisterKeyBinding(keyGestureHandler);
    }
  }

  private static void OnIsKeyGestureCommandExecutionEnabled(DependencyObject attachedElement, DependencyPropertyChangedEventArgs e)
  {
    if (attachedElement is not ICommandSource commandSource)
    {
      throw new ArgumentException($"Attached element must be of type {typeof(ICommandSource)}.");
    }

    KeyGesture keyGesture = GetSourceKeyGesture(attachedElement);
    if ((bool)e.NewValue)
    {
      RegisterCommandBinding(commandSource, keyGesture);
    }
    else
    {
      UnregisterCommandBinding(commandSource, keyGesture);
    }
  }

  private static void OnTargetKeyGesturesChanged(DependencyObject attachedElement, DependencyPropertyChangedEventArgs e)
  {
    if (attachedElement is not UIElement keyGestureHandler)
    {
      throw new ArgumentException($"Attached element must be of type {typeof(UIElement)}.");
    }

    if (e.OldValue is InputBindingCollection)
    {
      UnregisterKeyBinding(keyGestureHandler);
    }

    if (!GetIsKeyGestureDelegationEnabled(keyGestureHandler))
    {
      return;
    }
    RegisterKeyBinding(keyGestureHandler, e.NewValue as InputGestureCollection);
  }

  private static void OnSourceKeyGestureChanged(DependencyObject attachedElement, DependencyPropertyChangedEventArgs e)
  {
    if (attachedElement is not ICommandSource commandSource)
    {
      throw new ArgumentException($"Attached element must be of type {typeof(ICommandSource)}.");
    }

    UnregisterCommandBinding(commandSource, e.OldValue as KeyGesture);

    if (!GetIsKeyGestureCommandExecutionEnabled(attachedElement))
    {
        return;
    }
    RegisterCommandBinding(commandSource, e.NewValue as KeyGesture);
  }

  private static void ExecuteKeyGestureDelegateCommand(object commandParameter)
  {
    if (InputBindingSourceMap.TryGetValue(commandParameter as KeyGesture, out IList<ICommandSource> commandSources))
    {
      foreach (ICommandSource commandSource in commandSources)
      {
        ExecuteCommandSource(commandSource);
      }
    }
  }

  private static void ExecuteCommandSource(ICommandSource commandSource)
  {
    if (commandSource.Command is RoutedCommand routedCommand)
    {
      IInputElement commandTarget = commandSource.CommandTarget ?? commandSource as IInputElement;
      if (routedCommand.CanExecute(commandSource.CommandParameter, commandTarget))
      {
        routedCommand.Execute(commandSource.CommandParameter, commandTarget);
      }
    }
    else if (commandSource.Command?.CanExecute(parameter: commandSource.CommandParameter) ?? false)
    {
      commandSource.Command.Execute(commandSource.CommandParameter);
    }
  }

  private static void RegisterKeyBinding(UIElement keyGestureHandler, InputGestureCollection inputGestureCollection)
  {
    if (inputGestureCollection == null)
    {
      return;
    }

    IList<InputBinding>? inputBindings = new List<InputBinding>();
    InputBindingTargetMap.Add(keyGestureHandler, inputBindings);
    foreach (KeyGesture gesture in inputGestureCollection.OfType<KeyGesture>())
    {
      var inputBinding = new KeyBinding(KeyGestureDelegateCommand, gesture) { CommandParameter = gesture };
      keyGestureHandler.InputBindings.Add(inputBinding);
      inputBindings.Add(inputBinding);
    }
  }

  private static void UnregisterKeyBinding(UIElement keyGestureHandler)
  {
    if (InputBindingTargetMap.TryGetValue(keyGestureHandler, out IList<InputBinding>? inputBindings))
    {
      foreach (InputBinding inputBinding in inputBindings)
      {
        keyGestureHandler.InputBindings.Remove(inputBinding);
      }
      InputBindingTargetMap.Remove(keyGestureHandler);
    }
  }

  private static void RegisterCommandBinding(ICommandSource commandSource, KeyGesture keyGesture)
  {
    if (keyGesture == null)
    {
      return;
    }

    if (!InputBindingSourceMap.TryGetValue(keyGesture, out IList<ICommandSource>? commandSources))
    {
      commandSources = new List<ICommandSource>();
      InputBindingSourceMap.Add(keyGesture, commandSources);
    }
    commandSources.Add(commandSource);
  }

  private static void UnregisterCommandBinding(ICommandSource commandSource, KeyGesture keyGesture)
  {
    if (keyGesture == null)
    {
      return;
    }

    if (InputBindingSourceMap.TryGetValue(keyGesture, out IList<ICommandSource>? commandSources))
    {
      commandSources.Remove(commandSource);
      if (!commandSources.Any())
      {
        InputBindingSourceMap.Remove(keyGesture);
      }
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM