更改 TabControl 中的"Tab"键而不是"Ctrl+Tab"键



我想使用 tab 键而不是标准 ctrl ctrl tab

来切换选项卡

我的代码是

<TabControl>
      <TabItem Header="Section 1" Name="tabSection1">
          <ScrollViewer>
               <ContentPresenter Name="cntSection1" />
           </ScrollViewer>
      </TabItem>
      <TabItem Header="Section 2" Name="tabSection2">
          <ScrollViewer>
               <ContentPresenter Name="cntSection2" />
           </ScrollViewer>
      </TabItem>            
 </TabControl>
  <StackPanel>
     <Button Content="Save" Name="btnSave"  />
      <Button Content="Cancel" Name="btnCancel" IsCancel="True" />
   </StackPanel>

我的每个ContentPresenters中的每个UserControls都包含多个UI元素,例如TextboxesCheckboxes

到目前为止,我已经尝试过没有运气的跟随。

  <TabControl.InputBindings>
            <KeyBinding Key="Tab" Modifiers="Control" Command="EditingCommands.TabForward" />
  </TabControl.InputBindings>

 <TabControl KeyboardNavigation.TabNavigation="Continue" KeyboardNavigation.ControlTabNavigation="None">

我实现了类似的东西,我希望在当前专注于选项卡中的最后一个文本框时,将选项卡键更改选项卡。我使用了以下附件的行为:

using System;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace Core.Common
{
    public static class ChangeTabsBehavior
    {
        public static bool GetChangeTabs(DependencyObject obj)
        {
            return (bool)obj.GetValue(ChangeTabsBehaviorProperty);
        }
        public static void SetChangeTabs(DependencyObject obj, bool value)
        {
            obj.SetValue(ChangeTabsBehaviorProperty, value);
        }
        public static readonly DependencyProperty ChangeTabsBehaviorProperty =
            DependencyProperty.RegisterAttached("ChangeTabs",
                typeof(bool), typeof(ChangeTabsBehavior),
                new PropertyMetadata(false, OnChangeTabsChanged));
        private static void OnChangeTabsChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var textBox = (FrameworkElement) sender;
            var changeTabs = (bool) (e.NewValue);
            if (changeTabs)
                textBox.PreviewKeyDown += TextBoxOnPreviewKeyDown;
            else
                textBox.PreviewKeyDown -= TextBoxOnPreviewKeyDown;
        }
        private static void TextBoxOnPreviewKeyDown(object sender, KeyEventArgs keyEventArgs)
        {
            if (keyEventArgs.Key == Key.Tab)
            {
                var textBox = (FrameworkElement) sender;
                var tabControl = textBox.TryFindParent<TabControl>();
                if (tabControl.SelectedIndex == tabControl.Items.Count - 1)
                    tabControl.SelectedIndex = 0;
                else
                    tabControl.SelectedIndex++;
                keyEventArgs.Handled = true;
            }
        }
    }
}

请参阅http://www.hardcodet.net/2008/02/find-wpf-parent for tryfindparent方法

然后,您像这样将行为固定在XAML中:

<TextBox local:ChangeTabsBehavior.ChangeTabs="True"/>

您应该能够轻松地为您的需求修改它。

在此处提供更多有关行为的信息:http://www.jayway.com/2013/03/20/behaviors-in-wpf-introduction/

最新更新