在TabControl中更改“Tab”键而不是“Ctrl + Tab”键

时间:2012-10-25 07:23:17

标签: wpf wpf-controls focus tabcontrol key-bindings

我想使用 Tab 键而不是标准 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">

1 个答案:

答案 0 :(得分:0)

我实现了类似的功能,当标签中的最后一个文本框当前聚焦时,我希望tab键可以更改选项卡。我使用了以下附加行为:

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;
            }
        }
    }
}

有关TryFindParent方法的信息,请参阅http://www.hardcodet.net/2008/02/find-wpf-parent

然后,您可以像下面这样在XAML中附加行为:

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

您应该可以根据自己的需要轻松修改它。

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