2
更清晰。我需要知道哪個元素偷窺了焦點元素LostFocus事件中的焦點。事情是這樣的:獲取WPF中隱藏焦點的元素
- 我有3個按鈕:A,B和C
- 「按鈕」 具有焦點
- 「按鈕C」 點擊
- 「按鈕」 LostFocus事件被觸發
- 在那裏,我想知道「按鈕C」偷焦點(莫非也一直「按鈕B」)
讓我知道是否有做到這一點的方式。
謝謝!
更清晰。我需要知道哪個元素偷窺了焦點元素LostFocus事件中的焦點。事情是這樣的:獲取WPF中隱藏焦點的元素
讓我知道是否有做到這一點的方式。
謝謝!
您可以隨時檢查FocusManager.GetFocusedElement(dObj)以獲取給定DependencyObject中的焦點元素。所以,在上面您的方案,這將是這樣的:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<Style TargetType="Button">
<EventSetter Event="LostFocus" Handler="OnLostFocus"/>
</Style>
</Window.Resources>
<StackPanel>
<Button>Button1</Button>
<Button>Button2</Button>
<Button>Button3</Button>
</StackPanel>
</Window>
事件處理程序:
private void OnLostFocus(object sender, RoutedEventArgs e)
{
object focusedElement = FocusManager.GetFocusedElement(this);
if (focusedElement is Button)
Console.WriteLine(((Button)focusedElement).Content.ToString());
}
如果我不知道聚焦元素的類型是什麼。 – joshua 2012-11-07 10:25:44