0

我想使用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">
4

1 回答 1

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/

于 2014-11-13T18:38:04.363 回答