WPF equivalent to ActiveControl?

2019-07-05 03:42发布

问题:

In Forms, I can simple use ActiveControl.name to get the name of the active control. However in WPF this doesn't work. What is the WPF command equivalent to Activecontrol.name? I'll take C# or VB, either way. Thank you!

回答1:

There are a couple of possibilities though I'm not sure either is an exact match for ActiveControl:

  • Keyboard.FocusedElement: this static member tells you which element has the keyboard focus in the current application
  • FocusManager.GetFocusedElement(): this allows you to get the focused element within a given focus scope. Note that this cannot be used on arbitrary controls, only on controls which are focus scopes (i.e. have FocusManager.IsFocusScope set to true).

Do either of these sound like what you want?