24

以下情况。我有一个带有五个键绑定的 UserControl。当 TextBox 获得焦点时,UserControl 的键绑定停止触发..

有没有办法解决这个“问题”?

<UserControl.InputBindings>
    <KeyBinding Key="PageDown" Modifiers="Control" Command="{Binding NextCommand}"></KeyBinding>
    <KeyBinding Key="PageUp" Modifiers="Control" Command="{Binding PreviousCommand}"></KeyBinding>
    <KeyBinding Key="End" Modifiers="Control"  Command="{Binding LastCommand}"></KeyBinding>
    <KeyBinding Key="Home" Modifiers="Control" Command="{Binding FirstCommand}"></KeyBinding>
    <KeyBinding Key="F" Modifiers="Control" Command="{Binding SetFocusCommand}"></KeyBinding>
</UserControl.InputBindings>
<TextBox Text="{Binding FilterText, UpdateSourceTrigger=PropertyChanged}">
    <TextBox.InputBindings>
        <KeyBinding Gesture="Enter" Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type UserControl }}, Path=DataContext.FilterCommand}"></KeyBinding>
    </TextBox.InputBindings>
</TextBox>

似乎功能键(F1等)和ALT+[key]确实有效。我认为CTRLandSHIFT修饰符以某种方式“阻止”事件冒泡到 UserControl。

4

5 回答 5

54

某些输入绑定有效而某些无效的原因是 TextBox 控件捕获并处理了一些键绑定。例如,它处理CTRL+V用于粘贴,CTRL+Home用于转到文本的开头等。另一方面,其他组合键(例如CTRL+ F3)不由 TextBox 处理,因此它们会冒泡。

如果您只是想禁用 TextBox 的输入绑定,那很简单 - 您可以使用该ApplicationCommands.NotACommand命令,该命令将禁用默认行为。例如,在以下情况下,使用CTRL+粘贴V将被禁用:

<TextBox>
    <TextBox.InputBindings>
        <KeyBinding Key="V" Modifiers="Control" Command="ApplicationCommands.NotACommand" />
    </TextBox.InputBindings>
</TextBox>

然而,让它冒泡到用户控件有点棘手。我的建议是创建一个附加行为,该行为将应用于 UserControl,注册到它的PreviewKeyDown事件,并在它们到达 TextBox 之前根据需要执行它的输入绑定。当执行输入绑定时,这将优先于 UserControl。

我编写了一个实现此功能的基本行为来帮助您入门:

public class InputBindingsBehavior
{
    public static readonly DependencyProperty TakesInputBindingPrecedenceProperty =
        DependencyProperty.RegisterAttached("TakesInputBindingPrecedence", typeof(bool), typeof(InputBindingsBehavior), new UIPropertyMetadata(false, OnTakesInputBindingPrecedenceChanged));

    public static bool GetTakesInputBindingPrecedence(UIElement obj)
    {
        return (bool)obj.GetValue(TakesInputBindingPrecedenceProperty);
    }

    public static void SetTakesInputBindingPrecedence(UIElement obj, bool value)
    {
        obj.SetValue(TakesInputBindingPrecedenceProperty, value);
    }

    private static void OnTakesInputBindingPrecedenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ((UIElement)d).PreviewKeyDown += new KeyEventHandler(InputBindingsBehavior_PreviewKeyDown);
    }

    private static void InputBindingsBehavior_PreviewKeyDown(object sender, KeyEventArgs e)
    {
        var uielement = (UIElement)sender;

        var foundBinding = uielement.InputBindings
            .OfType<KeyBinding>()
            .FirstOrDefault(kb => kb.Key == e.Key && kb.Modifiers == e.KeyboardDevice.Modifiers);

        if (foundBinding != null)
        {
            e.Handled = true;
            if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
            {
                foundBinding.Command.Execute(foundBinding.CommandParameter);
            }
        }
    }
}

用法:

<UserControl local:InputBindingsBehavior.TakesInputBindingPrecedence="True">
    <UserControl.InputBindings>
        <KeyBinding Key="Home" Modifiers="Control" Command="{Binding MyCommand}" />
    </UserControl.InputBindings>
    <TextBox ... />
</UserControl>

希望这可以帮助。

于 2012-10-17T21:02:17.137 回答
4

Adi Lester 的解决方案效果很好。这是使用 Behavior 的类似解决方案。C# 代码:

public class AcceptKeyBinding : Behavior<UIElement>
{


    private TextBox _textBox;



    /// <summary>
    ///  Subscribes to the PreviewKeyDown event of the <see cref="TextBox"/>.
    /// </summary>
    protected override void OnAttached()
    {
        base.OnAttached();

        _textBox = AssociatedObject as TextBox;

        if (_textBox == null)
        {
            return;
        }

        _textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown;
    }

    private void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs keyEventArgs)
    {
        var uielement = (UIElement)sender;

        var foundBinding = uielement.InputBindings
            .OfType<KeyBinding>()
            .FirstOrDefault(kb => kb.Key == keyEventArgs.Key && kb.Modifiers ==           keyEventArgs.KeyboardDevice.Modifiers);

        if (foundBinding != null)
        {
            keyEventArgs.Handled = true;
            if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
            {
                foundBinding.Command.Execute(foundBinding.CommandParameter);
            }
        }
    }

    /// <summary>
    ///     Unsubscribes to the PreviewKeyDown event of the <see cref="TextBox"/>.
    /// </summary>
    protected override void OnDetaching()
    {
        if (_textBox == null)
        {
            return;
        }

        _textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown;

        base.OnDetaching();
    }

}

和 XAML:

<TextBox>
  <TextBox.InputBindings>
      <KeyBinding Key="Enter" Modifiers="Shift" Command="{Binding CommandManager[ExecuteCommand]}"
          CommandParameter="{Binding ExecuteText}" />
  </TextBox.InputBindings>
      <i:Interaction.Behaviors>
         <behaviours:AcceptKeyBinding />
      </i:Interaction.Behaviors>
</TextBox>
于 2014-05-02T02:14:18.343 回答
4

除了 Adi Lester 他的(非常有帮助的)回答之外,我还想提出一些改进/扩展来帮助我实施的建议。

手势匹配

foundBinding 也可以通过调用 Gesture.Matches 来完成。将 foundBinding Linq 查询更改为以下内容:

KeyBinding foundBinding = ((UIElement)this).InputBindings
            .OfType<KeyBinding>()
            .FirstOrDefault(inputBinding => inputBinding.Gesture.Matches(sender, eventArgs));

鼠标绑定

此外,您还可以定义 MouseBindings。

<MouseBinding Command="{Binding DataContext.AddInputValueCommand, ElementName=root}" CommandParameter="{Binding}" Gesture="Shift+MiddleClick" />

然后,您还需要订阅 PreviewMouseEvents,例如 PreviewMouseUp 和 PreviewMouseDoubleClick。实现与 KeyBindings 几乎相同。

private void OnTextBoxPreviewMouseUp(object sender, MouseButtonEventArgs eventArgs)
{
    MouseBinding foundBinding = ((UIElement)this).InputBindings
        .OfType<MouseBinding>()
        .FirstOrDefault(inputBinding => inputBinding.Gesture.Matches(sender, eventArgs));

    if (foundBinding != null)
    {
        eventArgs.Handled = true;
        if (foundBinding.Command.CanExecute(foundBinding.CommandParameter))
        {
            foundBinding.Command.Execute(foundBinding.CommandParameter);
        }
    }
}
于 2016-12-07T15:48:10.167 回答
2

这个线程很旧,但很多人都有这个问题。我的研究表明,Adi Lester 的解决方案是唯一一个不是“肮脏”的解决方法。对于需要的人,VisualBasic.NET 实现:

Public Class InputBindingsBehavior
    Public Shared ReadOnly TakesInputBindingPrecedenceProperty As DependencyProperty = DependencyProperty.RegisterAttached("TakesInputBindingPrecedence", GetType(Boolean), GetType(InputBindingsBehavior), New UIPropertyMetadata(False, AddressOf OnTakesInputBindingPrecedenceChanged))

    Public Shared Function GetTakesInputBindingPrecedence(obj As UIElement) As Boolean
        Return obj.GetValue(TakesInputBindingPrecedenceProperty)
    End Function

    Public Shared Sub SetTakesInputBindingPrecedence(obj As UIElement, value As Boolean)
        obj.SetValue(TakesInputBindingPrecedenceProperty, value)
    End Sub

    Public Shared Sub OnTakesInputBindingPrecedenceChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
        AddHandler DirectCast(d, UIElement).PreviewKeyDown, AddressOf InputBindingsBehavior_PreviewKeyDown
    End Sub

    Public Shared Sub InputBindingsBehavior_PreviewKeyDown(sender As Object, e As KeyEventArgs)
        Dim uielement = DirectCast(sender, UIElement)

        Dim foundBinding = uielement.InputBindings.OfType(Of KeyBinding).FirstOrDefault(Function(kb As KeyBinding) kb.Key = e.Key And kb.Modifiers = e.KeyboardDevice.Modifiers)

        If foundBinding IsNot Nothing Then
            e.Handled = True
            If foundBinding.Command.CanExecute(foundBinding.CommandParameter) Then
                foundBinding.Command.Execute(foundBinding.CommandParameter)
            End If
        End If
    End Sub

End Class

其余如前所述。

于 2020-04-08T10:36:06.477 回答
0
<UserControl.Style>
    <Style TargetType="UserControl">
        <Style.Triggers>
            <Trigger Property="IsKeyboardFocusWithin" Value="True">
                <Setter Property="FocusManager.FocusedElement" Value="   {Binding ElementName=keyPressPlaceHoler}" />
                </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>

keyPressPlaceHoler是目标容器的名称uielement

记得在用户控件中Focusable="True"设置

于 2016-06-12T18:10:57.453 回答