我正在使用具有可编辑内容的代码构建GUI.当用户点击静态显示时,控件将换出允许编辑的控件.在某些情况下,显示控件(例如Label)被换出来用于一组控件,例如ComboBox和TextBox.
我想检测何时从我的可编辑控件组中丢失焦点,以便将界面从编辑器切换回该项的显示表示.
例如,我可能在Panel1中有一个GUI(Panel2(Button1,Button2),Button3),我想检测何时从Panel2丢失焦点.我尝试了以下(F#代码):
open System.Windows let button1 = Controls.Button(Content="1") let button2 = Controls.Button(Content="2") let button3 = Controls.Button(Content="3") [<System.STAThreadAttribute>] do let panel1 = Controls.StackPanel() let panel2 = Controls.StackPanel() panel2.Children.Add button1 |> ignore panel2.Children.Add button2 |> ignore panel1.Children.Add panel2 |> ignore panel1.Children.Add button3 |> ignore panel2.LostFocus.Add(fun _ -> printfn "Panel2 lost focus") Application().Run(Window(Content=panel1)) |> ignore
单击button2之后单击button3时会触发panel2.LostFocus事件,正如预期的那样,因为焦点从panel1移出到button3.但是,即使panel2从未失去焦点,单击button1后单击button2也会触发它.
阅读有关WPF焦点的MSDN文档我尝试添加:
Input.FocusManager.SetIsFocusScope(panel2,true)
但这实际上使问题变得更糟!现在只有当焦点从panel2的一个子节点转移到另一个子节点而不是当panel2实际失去焦点时才会触发panel2.LostFocus事件.
我该如何获得理想的效果?
解决方法
感谢Twitter上的
Ian Voyce,我能够使用IsKeyboardFocusWithinChanged事件获得我需要的功能.这是一个演示:
open System.Windows let Button x = Controls.Button(Content=x,Width=64.0,Margin=Thickness 3.0) let Panel ctrls = let panel = Controls.StackPanel() for ctrl in ctrls do panel.Children.Add ctrl |> ignore panel let label = Controls.Label(Content="Edit 12") let button1 = Button "1" let button2 = Button "2" let button3 = Button "3" let panel = Panel[button1; button2] [<System.STAThreadAttribute>] do label.HorizontalContentAlignment <- HorizontalAlignment.Center label.MouseLeftButtonDown.Add(fun _ -> label.Content <- panel) panel.IsKeyboardFocusWithinChanged.Add(fun e -> if not(unBox e.NewValue) then label.Content <- "Edit 12") Application().Run(Window(Content=Panel[label :> UIElement; button3 :> UIElement])) |> ignore