Gha*_*han 2 debugging wpf focus
我有办法知道哪个项目有焦点和WPF应用程序?有没有办法监视wpf中的所有事件和方法调用?
FocusManager.GetFocusedElement(this); // where this is Window1
Run Code Online (Sandbox Code Playgroud)
这是一个完整的示例(当应用程序运行时,关注文本框并按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>
Run Code Online (Sandbox Code Playgroud)
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)