有没有办法知道在和WPF应用程序中哪一项是焦点?有没有办法监控wpf中的所有事件和方法调用?
发布于 2009-07-27 15:41:37
好的,在这个页面上,你会找到一个解决方案,但如果你问我,它有点脏:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/a420dc50-b238-4d2e-9209-dfbd98c7a060
它使用VisualTreeHelper创建一个包含所有控件的大列表,然后通过查看IsFocused属性来询问它们是否具有焦点。
我认为有一种更好的方法。也许可以搜索一下主动控件或结合WPF的焦点控件。
编辑:这个主题可能对How to programmatically navigate WPF UI element tab stops?很有用
发布于 2009-07-27 15:39:27
FocusManager.GetFocusedElement(this); // where this is Window1这是一个完整的示例(当应用程序运行时,将焦点放在文本框中并按enter键)
xaml:
<Window x:Class="StackOverflowTests.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" KeyDown="window1_KeyDown"
Title="Window1" x:Name="window1" Height="300" Width="300">
<StackPanel>
<TextBox x:Name="textBox1" />
<TextBox x:Name="textBox2" />
<TextBox x:Name="textBox3" />
<TextBox x:Name="textBox4" />
<TextBox x:Name="textBox5" />
</StackPanel>
</Window>C#
using System.Windows;
using System.Windows.Input;
namespace StackOverflowTests
{
/// <summary>
/// Interaction logic for Window1.xaml
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
private void window1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if(e.Key == Key.Return)
MessageBox.Show((FocusManager.GetFocusedElement(this) as FrameworkElement).Name);
}
}
}https://stackoverflow.com/questions/1188873
复制相似问题