我有一个文本框,里面有这个:
<KeyBinding Command="{Binding MyCommand}" Key="Tab"/>
问题是它吞下了 Tab 并且没有 Tab 到下一个控件。如何捕获文本框的选项卡并仍然保留选项卡到选项卡顺序中的下一个控件?编辑:我也在使用 MVVM 并且 MyCommand 在 ViewModel 代码中,所以这就是我需要重新抛出 Tab 的地方。
我有一个文本框,里面有这个:
<KeyBinding Command="{Binding MyCommand}" Key="Tab"/>
问题是它吞下了 Tab 并且没有 Tab 到下一个控件。如何捕获文本框的选项卡并仍然保留选项卡到选项卡顺序中的下一个控件?编辑:我也在使用 MVVM 并且 MyCommand 在 ViewModel 代码中,所以这就是我需要重新抛出 Tab 的地方。
这很容易实现,只是不要为此使用 KeyBinding。处理您的 TextBox 的 OnKeyDown 事件:
<TextBox KeyDown="UIElement_OnKeyDown" ...
然后在代码隐藏中,每当按下 Tab 时执行您的命令。与 KeyBinding 不同,这不会吞噬 TextInput 事件,因此它应该可以工作。
private void OnKeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Tab:
// Execute your command. Something similar to:
((YourDataContextType)DataContext).MyCommand.Execute(parameter:null);
break;
}
}
为什么不在命令处理程序中使用此代码?
private void MyCommandHandler(){
// Do command's work here
TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
request.Wrapped = true;
control.MoveFocus(request);
}
这基本上就是'Tab'所做的,所以如果你也这样做,你就可以开始了。(如果你有一个带有 Shift-Tab 的命令,当然可以反转方向。
我实际上将它包装成这样的扩展方法......
public static class NavigationHelpers{
public static void MoveFocus(this FrameworkElement control, FocusNavigationDirection direction = FocusNavigationDirection.Next, bool wrap = true) {
TraversalRequest request = new TraversalRequest(direction);
request.Wrapped = wrap;
control.MoveFocus(request);
}
}
...意味着先前的代码变得更加简单,就像这样...
private void MyCommandHandler(){
// Do command's work here
Control.MoveFocus();
}
...如果您不知道当前的焦点控制是什么,您可以这样做...
(Keyboard.FocusedElement as FrameworkElement).MoveFocus();
希望这可以帮助!如果是这样,如果您投票给我或将其标记为已接受,将不胜感激!
鉴于您的问题是纯 XAML 解决方案,我找不到将焦点设置为控件的方法。
我选择创建一个attacted 属性,然后通过绑定将焦点从与ViewModel 中的KeyBinding 关联的Command 设置为下一个控件。
这是视图:
<Window x:Class="WarpTab.Views.MainView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:WarpTab.Commands"
xmlns:Views="clr-namespace:WarpTab.Views"
xmlns:local="clr-namespace:WarpTab.ViewModels"
Title="Main Window" Height="400" Width="800">
<Window.Resources>
<c:CommandReference x:Key="MyCommandReference" Command="{Binding MyCommand}" />
</Window.Resources>
<DockPanel>
<ScrollViewer>
<WrapPanel >
<TextBox Text="First text value" >
<TextBox.InputBindings>
<KeyBinding Command="{StaticResource MyCommandReference}" Key="Tab"/>
</TextBox.InputBindings>
</TextBox>
<TextBox Text="Next text value" local:FocusExtension.IsFocused="{Binding FocusControl}" />
<Button Content="My Button" />
</WrapPanel>
</ScrollViewer>
</DockPanel>
</Window>
这是视图模型:
using System.Windows.Input;
using WarpTab.Commands;
namespace WarpTab.ViewModels
{
public class MainViewModel : ViewModelBase
{
public ICommand MyCommand { get; set; }
public MainViewModel()
{
MyCommand = new DelegateCommand<object>(OnMyCommand, CanMyCommand);
}
private void OnMyCommand(object obj)
{
FocusControl = true;
// process command here
// reset to allow tab to continue to work
FocusControl = false;
return;
}
private bool CanMyCommand(object obj)
{
return true;
}
private bool _focusControl = false;
public bool FocusControl
{
get
{
return _focusControl;
}
set
{
_focusControl = value;
OnPropertyChanged("FocusControl");
}
}
}
}
这是定义我在以下答案中找到的附加属性的代码。
using System.Windows;
namespace WarpTab.ViewModels
{
public static class FocusExtension
{
public static bool GetIsFocused(DependencyObject obj)
{
return (bool)obj.GetValue(IsFocusedProperty);
}
public static void SetIsFocused(DependencyObject obj, bool value)
{
obj.SetValue(IsFocusedProperty, value);
}
public static readonly DependencyProperty IsFocusedProperty =
DependencyProperty.RegisterAttached(
"IsFocused", typeof(bool), typeof(FocusExtension),
new UIPropertyMetadata(false, OnIsFocusedPropertyChanged));
private static void OnIsFocusedPropertyChanged(DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
var uie = (UIElement)d;
if ((bool)e.NewValue)
{
uie.Focus(); // Don't care about false values.
}
}
}
}
遇到了同样的问题,遇到了这个帖子,花了我一段时间才找到最佳答案。参考:在特定键上使用 EventTrigger 定义此类:
using System; using System.Windows.Input; using System.Windows.Interactivity;
public class KeyDownEventTrigger : EventTrigger
{
public KeyDownEventTrigger() : base("KeyDown")
{
}
protected override void OnEvent(EventArgs eventArgs)
{
var e = eventArgs as KeyEventArgs;
if (e != null && e.Key == Key.Tab)
{
this.InvokeActions(eventArgs);
}
}
}
文本框的 xaml:
<TextBox x:Name="txtZip"
Text="{Binding Zip, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<TextBox.InputBindings>
<KeyBinding Key="Enter" Command="{Binding ZipLookup.GetAddressByZipKeyCommand}" CommandParameter="{Binding ElementName=txtZip, Path=Text}" />
</TextBox.InputBindings>
<i:Interaction.Triggers>
<iCustom:KeyDownEventTrigger EventName="KeyDown">
<i:InvokeCommandAction Command="{Binding ZipLookup.GetAddressByZipKeyCommand}" CommandParameter="{Binding ElementName=txtZip, Path=Text}" />
</iCustom:KeyDownEventTrigger>
</i:Interaction.Triggers>
</TextBox>
在您的窗口或用户控件根标记中包含以下属性:
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:iCustom="clr-namespace:[NAMESPACE FOR CUSTOM KEY DOWN CLASS]"