问题描述
我在网格中有一些按钮,并且想使用箭头键在它们之间导航.我很难让程序在打开时以及在使用TAB使用箭头键开始导航后将注意力集中在按钮上时,我在定向时失去控制 将焦点对准没有按钮的方向.
I have buttons in a grid and would like to navigate between them using arrow keys. I am having a hard time getting the program to focus on a button when it opens and also when after using TAB to start navigation with arrow keys I lose control when directing focus in a direction that does no have a Button.
我是C#的新手,也是WPF的新手,我正在尝试为HTPC编写一个程序,以使其尽可能对用户友好,因此使用键盘进行的导航将来自游戏或专用.控制器.
I am new to C# and newer to WPF and I am trying to make a program for my HTPC which I am trying to make as user friendly as possible hence the navigation with keyboard which input will be coming from a game and or dedicated controller.
我也获得了Button导航.我想包括一个ListBox以及可能用于不同设置等的更多窗口.
Also after I get the Button nav. I want to include a ListBox and possibly more windows for different settings and such.
在此先感谢您的帮助!
推荐答案
>>我很难让程序在打开时将焦点放在按钮上
>> I am having a hard time getting the program to focus on a button when it opens
要在WPF中设置默认的焦点控件,我们可以使用 FocusManager.FocusedElement xaml中的附加属性:
To set the default focused control in WPF, we can use FocusManager.FocusedElementattached property in xaml:
#FocusManager.FocusedElement附加属性
http://msdn.microsoft.com /en-us/library/system.windows.input.focusmanager.focusedelement(v=vs.110).aspx
示例:
<Grid FocusManager.FocusedElement="{Binding ElementName=button1}">
<Button Name="button1" Width="100" Height="40" Content="Button1" Margin="-150,-50,0,0" Click="button_Click" />
<Button Name="button2" Width="100" Height="40" Content="Button2" Margin="0,-50,-150,0" Click="button_Click" />
<Button Name="button3" Width="100" Height="40" Content="Button3" Margin="-150,0,0,-100" Click="button_Click" />
<Button Name="button4" Width="100" Height="40" Content="Button4" Margin="0,0,-150,-100" Click="button_Click" />
</Grid>
上面的代码将在程序打开时集中在第一个按钮上.
The above code will focus on the first button when the program opens.
>>并且在使用TAB使用箭头键开始导航之后,将焦点指向没有按钮的方向时,我也失去控制.
>> and also when after using TAB to start navigation with arrow keys I lose control when directing focus in a direction that does no have a Button.
对于此问题,请提供可复制的示例以进行故障排除:)
For this issue, please provide a reproducible sample for troubleshooting:)
这篇关于方向键导航帮助,请的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!