如何在基于WPF的应用程序中的Windows窗体上实现Tab键顺序 [英] How do I implement tab order on windows form in a WPF based application

查看:66
本文介绍了如何在基于WPF的应用程序中的Windows窗体上实现Tab键顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hi
我正在使用C#开发基于WPF的应用程序。该应用程序包含一个基于Windows的窗体包含许多控件。我需要在基于Windows的表单上实现Tab顺序。

如何获得。请记住,Application是基于WPF的,而Form是基于Windows的Form。

谢谢



我尝试了什么:



我通过设置Tab Stop属性并为所有控件定义Tab index属性来遵循规则。但它不适用于基于WPF的应用程序。

但它适用于基于Windows窗体的应用程序。

解决方案

设置Tabindex应该在WPF以及WinForms。您可以在XAML中进行设置。例如

<按钮x:名称=Button1TabIndex =2高度=60宽度=75边距=221,26,221,20Grid.Row =0/> ; 
<按钮x:名称=Button2TabIndex =1高度=60宽度=75边距=221,26,221,20Grid.Row =1/>
<按钮x:名称=Button3TabIndex =0高度=60宽度=75边距=221,26,221,20Grid.Row =2/>

或者在每个控件的属性窗口中。或者在代码背后,例如

 Button1.TabIndex =  2 ; 
Button2.TabIndex = 0 ;
Button3.TabIndex = 1 ;

在正常情况下,WPF将根据逻辑树( Tab Order |你应该知道的关于WPF的2000件事情事项 [ ^ ])。如果它没有按预期运行你是否适当地迎合了容器控件?



可能最好的方法是不要使用TabIndex,而是剪切和粘贴你的代码所以控件按照您希望它们具有tabindex的顺序在XAML中排序,例如

<按钮x:名称=Button2高度=60宽度=75边距=221,26,221 ,20Grid.Row =1/> 
<按钮x:名称=Button1高度=60宽度=75边距=221,26,221,20Grid.Row =0/>
<按钮x:名称=Button3高度=60宽度=75保证金=221,26,221,20Grid.Row =2/>

请注意,在删除TabIndex时,可能需要检查属性(在属性窗口中)是否已恢复为默认值2147483647.



如果仍然无法正常工作如您所愿,然后尽可能地简化您的表单以仍然演示问题并使用改善问题链接发布XAML





这个(整个)代码演示了控件在XAML中出现的顺序,用于确定选项卡时的标签顺序索引本身未更改(即属性窗口的选项卡索引默认值= 2147483647)

 <  窗口    x:Class   =  MainWindow    

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

标题 = MainWindow 高度 = 350 宽度 = 525 >
< 网格 >
< Grid.RowDefinitions > < RowDefinition / > < RowDefinition / > < < span class =code-leadattribute> RowDefinition / > < / Grid.RowDefinitions >
< Grid.ColumnDefinitions > < ColumnDefinition / > < ColumnDefinition / > < ColumnDefinition / > < / Grid.ColumnDefinit离子 >
< TextBox x:名称 = TextBox1 Grid.Column = 0 Grid.Row = 0 文字 = TextBox1 / >
< TextBox x:姓名 = TextBox2 Grid.Column = 2 Grid.Row = 1 文字 = TextBox2 / >
< TextBox x:名称 = TextBox3 Grid.Column = 1 Grid.Row = 1 文字 = TextBox3 / >
< TextBox x:名称 = TextBox4 Grid.Column = 1 Grid.Row = 0 文本 = TextBox4 / >
< TextBox x:名称 = TextBox5 Grid.Column = 2 Grid.Row = 2 文字 = TextBox5 / >
< ; TextBox x:名称 = TextBox6 Grid.Column = 0 Grid.Row = 2 文字 = TextBox6 / >
< / Grid >
< / Window >

文本框中的文字显示了我期望控件在我使用时获得焦点的顺序标签键。首次加载时,表单本身看起来有点像这样:

 TextBox1 TextBox4(BlankCell)

(BlankCell)TextBox3 TextBox2

TextBox6(BlankCell)TextBox5

此版本的代码具有以相反顺序标记的文本框,即TextBox6是顺序中的第一个,然后是5,4,3,2,1 < pre lang =HTML> < 窗口 x:Class = MainWindow

xmlns = http://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x = http://schemas.microsoft.com/winfx/2006/xaml

标题 = MainWindow 高度 = 350 宽度 = 525 >
< 网格 >
< Grid.RowDefinitions > < RowDefinition / > < RowDefinition / > < RowDefinition / > < / Grid.RowDefinitions >
< Grid.ColumnDefinitions > < span class =code-keyword><
ColumnDefinition / > < ColumnDefinition / > < ColumnDefinition / > < / Grid.ColumnDefinitions >
< TextBox x:名称 = TextBox1 G. rid.Column = 0 Grid.Row = 0 文本 = TextBox1 TabIndex = 5 / >
< TextBox x:名称 = TextBox2 Grid.Column = 2 Grid.Row = 1 文本 = TextBox2 TabIndex = 4 / >
< TextBox < span class =code-attribute>
x:名称 = TextBox3 Grid.Column = 1 Grid.Row = 1 文字 = TextBox3 TabIndex = 3 / >
< TextBox x:名称 = TextBox4 Grid.Column = 1 Grid.Row = 0 文字 = TextBox4 TabIndex = 2 / >
< TextBox x:名称 = TextBox5 < span class =code-attribute> Grid.Column = 2 Grid.Row = 2 文字 = TextBox5 TabIndex = 1 / >
< TextBox x:名称 = TextBox6 Grid.Column = 0 Grid.Row = 2 文字 = TextBox6 TabIndex = 0 / >
< / Grid >
< / Window >

如果我在加载表单时将XAML TabIndex设置替换为后面的代码,则表单的行为是相同的。注意项目中没有其他代码(除了自动生成的Application.xaml等),我的目标是.NET Framework 3.5。



如果您的问题是在第一次点击Tab键之后才选择第一个TextBox,然后尝试:

 <   Grid     FocusManager.FocusedElement   =  {Binding ElementName = TextBox1} >  

注意行为的改变 - 使用此FocusManager TextBox1在表单加载后具有焦点,Tab键将我们移动到TextBox6。如果没有FocusManager,在加载表单时没有任何焦点,并且Tab键将我们直接移动到TextBox6


Hi I am working on a WPF based application in C#. The application contains a Windows based Form containing many controls. I need to implement Tab order on the Windows based Form.
How can this be obtained. Remember that Application is WPF based while Form is Windows based Form.
Thanks

What I have tried:

I have followed rules by setting Tab Stop property and defining Tab index property for all controls. But it doesn't work for WPF based application.
However it works on Windows Form based application.

解决方案

Setting Tabindex should work in WPF as well as WinForms. You can set it in the XAML. E.g

<Button x:Name="Button1" TabIndex="2" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="0"/>
<Button x:Name="Button2" TabIndex="1" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="1"/>
<Button x:Name="Button3" TabIndex="0" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="2"/>

Or in the Properties window of each control. Or in the Code behind e.g.

Button1.TabIndex = 2;
Button2.TabIndex = 0;
Button3.TabIndex = 1;

Under normal circumstances WPF will assign tab order based on the logical tree (Tab Order | 2,000 Things You Should Know About WPF[^]). If it is not behaving as expected have you catered for container controls properly?

Probably the best way would be to not use TabIndex at all but cut&paste your code so that the controls are ordered in the XAML in the order you wish them to have tabindex e.g.

<Button x:Name="Button2" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="1"/>
<Button x:Name="Button1" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="0"/>
<Button x:Name="Button3" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="2"/>

Note when removing a TabIndex it may be worth checking that the Property (in the Properties window) has reverted to the default value of 2147483647.

If it is still not working as you wish, then simplify your form as much as possible to still demonstrate the problem and post the XAML by using the Improve question link

[EDIT - As requested some "full" solutions]
This (entire) code demonstrates that it is the order that the controls appear in the XAML that determines tab order when tab index itself is not changed (i.e. the Properties window has tab index default = 2147483647)

<Window x:Class="MainWindow" 

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
        <Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>
        <TextBox x:Name="TextBox1" Grid.Column="0" Grid.Row="0" Text="TextBox1" />
        <TextBox x:Name="TextBox2" Grid.Column="2" Grid.Row="1" Text="TextBox2" />
        <TextBox x:Name="TextBox3" Grid.Column="1" Grid.Row="1" Text="TextBox3" />
        <TextBox x:Name="TextBox4" Grid.Column="1" Grid.Row="0" Text="TextBox4" />
        <TextBox x:Name="TextBox5" Grid.Column="2" Grid.Row="2" Text="TextBox5" />
        <TextBox x:Name="TextBox6" Grid.Column="0" Grid.Row="2" Text="TextBox6" />
    </Grid>
</Window>

The text in the textboxes shows the order that I am expecting the controls to get focus when I use the tab key. The form itself looks a little like this when first loaded:

TextBox1            TextBox4         (BlankCell)

(BlankCell)         TextBox3         TextBox2

TextBox6            (BlankCell)      TextBox5

This version of the code has the textboxes being tabbed into in reverse order i.e. TextBox6 is the first in the order, then 5, 4, 3, 2, 1

<Window x:Class="MainWindow" 

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
        <Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>
        <TextBox x:Name="TextBox1" Grid.Column="0" Grid.Row="0" Text="TextBox1" TabIndex ="5"/>
        <TextBox x:Name="TextBox2" Grid.Column="2" Grid.Row="1" Text="TextBox2" TabIndex ="4"/>
        <TextBox x:Name="TextBox3" Grid.Column="1" Grid.Row="1" Text="TextBox3" TabIndex ="3"/>
        <TextBox x:Name="TextBox4" Grid.Column="1" Grid.Row="0" Text="TextBox4" TabIndex ="2"/>
        <TextBox x:Name="TextBox5" Grid.Column="2" Grid.Row="2" Text="TextBox5" TabIndex ="1"/>
        <TextBox x:Name="TextBox6" Grid.Column="0" Grid.Row="2" Text="TextBox6" TabIndex ="0"/>
    </Grid>
</Window>

Behaviour of the form is the same if I substitute the XAML TabIndex setting for code behind when loading the form. Note there is no other code in the project (other than the automatically generated Application.xaml etc) and I targeted .NET Framework 3.5.

If your issue is that the first TextBox is not being selected until after hitting the Tab key for the first time then try this:

<Grid FocusManager.FocusedElement="{Binding ElementName=TextBox1}">

Notice the change in behaviour - with this FocusManager TextBox1 has focus once the form has loaded and the Tab key moves us to TextBox6. Without the FocusManager nothing has focus when the form is loaded and the Tab key moves us straight to TextBox6


这篇关于如何在基于WPF的应用程序中的Windows窗体上实现Tab键顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆