Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 更改“;标签";键而不是“;Ctrl+;选项卡”;TabControl中的键_Wpf_Wpf Controls_Focus_Tabcontrol_Key Bindings - Fatal编程技术网

Wpf 更改“;标签";键而不是“;Ctrl+;选项卡”;TabControl中的键

Wpf 更改“;标签";键而不是“;Ctrl+;选项卡”;TabControl中的键,wpf,wpf-controls,focus,tabcontrol,key-bindings,Wpf,Wpf Controls,Focus,Tabcontrol,Key Bindings,我想使用Tab键而不是标准的Ctrl+Tab键切换选项卡 我的代码是 <TabControl> <TabItem Header="Section 1" Name="tabSection1"> <ScrollViewer> <ContentPresenter Name="cntSection1" /> </ScrollViewer> </

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


我实现了类似的功能,当选项卡中的最后一个文本框当前处于焦点时,我希望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;
            }
        }
    }
}
使用系统;
使用System.Linq;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Input;
名称空间核心.Common
{
公共静态类变更tabsbehavior
{
公共静态bool getchangetab(DependencyObject对象对象)
{
返回(bool)对象获取值(changetablehaviorproperty);
}
公共静态无效SetChangeTabs(DependencyObject对象,布尔值)
{
对象设置值(ChangeTabsBehaviorProperty,值);
}
公共静态只读从属属性更改选项卡行为属性=
DependencyProperty.RegisterAttached(“ChangeTabs”,
typeof(bool),typeof(changetabehavior),
新的PropertyMetadata(错误,一旦更改);
私有静态void OnChangeTabsChanged(对象发送方,DependencyPropertyChangedEventArgs e)
{
var textBox=(FrameworkElement)发送方;
var changetab=(bool)(即NewValue);
如果(更改选项卡)
textBox.PreviewKeyDown+=TextBoxOnPreviewKeyDown;
其他的
textBox.PreviewKeyDown-=TextBoxOnPreviewKeyDown;
}
私有静态void TextBoxOnPreviewKeyDown(对象发送方,KeyEventArgs KeyEventArgs)
{
if(keyEventArgs.Key==Key.Tab)
{
var textBox=(FrameworkElement)发送方;
var tabControl=textBox.TryFindParent();
if(tabControl.SelectedIndex==tabControl.Items.Count-1)
tabControl.SelectedIndex=0;
其他的
tabControl.SelectedIndex++;
keyEventArgs.Handled=true;
}
}
}
}
有关TryFindParent方法,请参见

然后在XAML中附加行为,如下所示:

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

您应该能够根据自己的需要很容易地修改它


更多关于行为的信息:

只是一个奇怪的问题。。。当当前的
TabItem
实际出现在内容视图中,并且该内容具有文本框等可聚焦控件时,您希望它的行为如何。因此,当我们单击
Tab
键时,您希望选择下一个
TabItem
,还是希望
Tab
键聚焦当前内容视图中的控件?当您要求这样的行为时,这一考虑应该很重要。当按下Tab键时,它应该将焦点移动到当前内容视图中的控件。当到达最后一个项目时,下一个TabItem应该可见。我建议使用自定义行为来实现这一点
TabControl
使用
Control+Tab键在
TabItems
之间切换的功能是特定的。我怀疑是否有人可以使用任何设置来覆盖它。
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;
            }
        }
    }
}
<TextBox local:ChangeTabsBehavior.ChangeTabs="True"/>