我正在使用WPF创建一个桌面应用程序,其中我有一个带有几个按钮的窗口和一个带有动态创建的多个选项卡项的选项卡控件。每个标签项都包含很多控件。
我想在选项卡按下当前标签项上的最后一个控件后移动到下一个标签项。现在它只是遍历整个窗口和相同标签项的所有控件。我设置了标签索引,我尝试设置KeyboardNavigation.TabNavigation =“一次”,还有本地,循环,继续等。但它没有帮助。
如下图所示,当我按下“逮捕日期”中的标签时,我想移动到“德州”标签上的第一个控件,但它将返回到同一选项卡上的“名称”控件。
答案 0 :(得分:1)
按static File f1;
static PrintWriter pw;
static void levelWithLargestNumberOfNodes(Lab5BTNode root) throws Exception
{
f1 = new File("temp.txt");
pw = new PrintWriter("temp.txt");
boolean b1 = f1.createNewFile();
//System.out.println(b1);
if(!b1){
for(int i = 0 ; i < (one.height(root)); i++)
{
printElementsOnALevel(one.root,i);
pw.println();
}
pw.flush(); pw.close();
}
boolean b2 = f1.exists();
//System.out.println(b2);
if(b2)
{
Scanner sc = new Scanner(f1);
int count = 0;
String lev0="",lev1="",lev2="",lev3="",lev4="",lev5="",lev6="",lev7 = "",lev8 = "",lev9 = "",lev10 = "";
while (sc.hasNextLine())
{
if(count==0)
lev0 = sc.nextLine();
if(count==1)
lev1 = sc.nextLine();
if(count==2)
lev2 = sc.nextLine();
if(count==3)
lev3 = sc.nextLine();
if(count==4)
lev4 = sc.nextLine();
if(count==5)
lev5 = sc.nextLine();
if(count==6)
lev6 = sc.nextLine();
if(count==7)
lev7 = sc.nextLine();
if(count==8)
lev8 = sc.nextLine();
if(count==9)
lev9 = sc.nextLine();
if(count==10)
lev10 = sc.nextLine();
count++;
}
StringTokenizer tokens = new StringTokenizer(lev0," ");
int firstcount = tokens.countTokens();
//System.out.println(firstcount);
StringTokenizer tokens1 = new StringTokenizer(lev1," ");
int secondcount = tokens1.countTokens();
StringTokenizer tokens2 = new StringTokenizer(lev2," ");
int thirdcount = tokens2.countTokens();
StringTokenizer tokens3 = new StringTokenizer(lev3," ");
int fourthcount = tokens3.countTokens();
StringTokenizer tokens4 = new StringTokenizer(lev4," ");
int fifthcount = tokens4.countTokens();
StringTokenizer tokens5 = new StringTokenizer(lev5," ");
int sixthcount = tokens5.countTokens();
StringTokenizer tokens6 = new StringTokenizer(lev6," ");
int seventhcount = tokens6.countTokens();
StringTokenizer tokens7 = new StringTokenizer(lev7," ");
int eighthcount = tokens7.countTokens();
StringTokenizer tokens8 = new StringTokenizer(lev8," ");
int ninthcount = tokens8.countTokens();
StringTokenizer tokens9 = new StringTokenizer(lev9," ");
int tenthcount = tokens9.countTokens();
StringTokenizer tokens10 = new StringTokenizer(lev10," ");
int eleventhcount = tokens10.countTokens();
int temp[] = {firstcount,secondcount,thirdcount,fourthcount
,fifthcount,sixthcount,seventhcount,eighthcount,ninthcount,tenthcount,eleventhcount};
int maxValue = temp[0];
for(int i=1;i < temp.length;i++)
{
if(temp[i] > maxValue)
{
maxValue = temp[i];
}
}System.out.println("Levels with max number of Nodes i.e "+maxValue+" nodes are as follows : " );
if(maxValue == firstcount)
System.out.println(lev0);
if(maxValue == secondcount)
System.out.println(lev1);
if(maxValue == thirdcount)
System.out.println(lev2);
if(maxValue == fourthcount)
System.out.println(lev3);
if(maxValue == fifthcount)
System.out.println(lev4);
if(maxValue == sixthcount)
System.out.println(lev5);
if(maxValue == seventhcount)
System.out.println(lev6);
if(maxValue == eighthcount)
System.out.println(lev7);
if(maxValue == ninthcount)
System.out.println(lev8);
if(maxValue == tenthcount)
System.out.println(lev9);
if(maxValue == eleventhcount)
System.out.println(lev10);
f1.delete();
}
}
时,将显示下一个选项卡,该选项卡中的第一个控件将具有焦点。要向后导航,请使用Control+Tab
。
答案 1 :(得分:1)
您的标签必须设置Name
属性集x:Name
中的XAML
。选项卡上的“最后”项目还必须设置Name
属性。
现在设置TabControl
对象的KeyDown
(或KeyUp
)事件:
Debug.WriteLine("key pressed: " + e.Key);
if (e.Key == Key.Tab)
{
if (btnButton.IsFocused) tab2.Focus();
}
Debug.WriteLine(...)
只是为了确保在一切正常工作后捕获,评论或删除该行。
按顺序关注选项卡需要一些额外的代码行。以下是完整的KeyDown
事件:
private void tabControl_KeyDown(object sender, KeyEventArgs e)
{
Debug.WriteLine("key pressed: " + e.Key);
if (e.Key == Key.Tab)
{
var tabs = tabControl.Items.Cast<TabItem>().ToArray();
var tab=tabs.Select((t, i)=>new {i, t}).SingleOrDefault(t => t.t.IsSelected); // one has to be selected. result is null of no tabs are present (then why have a tab control?)
if (tab == null) return;
switch (tab.i)
{
case 0:
if (btnButton.IsFocused)
tab2.Focus();
break;
case 1:
tab3.Focus();
break;
case 2:
tab4.Focus();
break;
case 3:
tab1.Focus(); // jump to first?
// OR do something else
break;
default:
break;
}
}
}
将btnButton
替换为表单上的“最后”项目。