I have a grid with SAVE button. Then I have a dockmanager which has 2 tabgrouppanes. The first tabgrouppane has a content pane which has a title textbox and few other fields.
Binding for title seems to happen only when we TAB out of the text box. Hitting SAVE without leaving the textbox does not do the binding. How do I get the textbox to receive a "lost focus" when I click on the SAVE button. Attaching sample code.
Thanks.
<Grid> <Grid x:Name="Grid_MenuBar"> <Button x:Name="Save" Grid.Column="1" Grid.Row="0" Margin="5" </Grid>
<igDock:XamDockManager Grid.Row="1" HorizontalAlignment="Stretch" VerticalAlignment="Stretch"> <igDock:XamDockManager.Panes> <igDock:SplitPane igDock:XamDockManager.InitialLocation="DockedTop" SplitterOrientation="Horizontal"> <igDock:TabGroupPane> <igDock:ContentPane Header="Primary"> <Grid x:Name="Song"> <TextBlock x:Name="SongTitle_txt" Text="Title" Grid.Row="1" Grid.Column="2" Margin="0,0,5,0" /> <TextBox x:Name="SongTitle_box" BorderThickness="0.5,0.5,0.5,0.5" Grid.Row="1" Grid.Column="3" Text="{Binding Path=Title, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}}"/> </igDock:ContentPane> </igDock:TabGroupPane> </igDock:SplitPane> </igDock:XamDockManager.Panes></Grid>
In WPF, LostFocus is not the act of losing keyboard focus but the act of losing logical focus. In the case of the XamDockManager, the ContentPane is a FocusScope and as such each pane has its own logically focused element. So you would need to change the FocusManager.FocusedElement of the ContentPane to another element within the pane. Note, this is the same type issue that you would have if you had a TextBox on the Window and clicked on a Save button within a Toolbar or a Save button within a Menu or XamRibbon as all of those are focus scopes so that interacting within focus within those elements does not interfere with or manipulate the item that is focused in the other area. Alternatively you could change the Binding such that the UpdateSourceTrigger is PropertyChanged so that the value in the underlying source is updated as you type.
Thanks. That worked.
WPF has 2 types of Focus - Keyboard Focus and Logical Focus. These are explained in MS' documentation regarding Focus.
For Keyboard Focus there is only ever 1 element that has keyboard focus - that is the one that is receiving the keyboard input. This is reflected via the Keyboard.FocusedElement property. There are LostKeyboardFocus and GotKeyboardFocus that relate to when an element (or one of its descendants) loses or receives the keyboard focus.
For Logical Focus, every Focus Scope has it's own focused element. This is exposed via the attached FocusManager.FocusedElement. Any element that is marked as a FocusScope (e.g. Window, Toolbar, Menu, Ribbon, ContentPane, etc.) will have its own focused element. The LostFocus and GotFocus events relate to when an element (or one of its descendants) becomes or is no longer the focused element for its containing focus scope. When you shift keyboard focus from one focus scope to another (e.g. click on a menu item, a toolbar, or into another contentpane) then these events are not triggered for the element that had the keyboard focus because it is still the FocusedElement of its containing FocusScope.
So what I was saying was that if you want the LostFocus of an element to occur that you would need to change the FocusedElement of its FocusScope as that is the only time that the WPF framework will raise the GotFocus/LostFocus events. There isn't really an ideal place to do that though especially since I believe the WPF framework will try to change the keyboard focus when you change the focused element of a focus scope. At least for the scenario raised by the original poster, the best option would be to set the UpdateSourceTrigger to PropertyChanged.
Note even if the ContentPane wasn't a FocusScope (and instead it belonged to the focusscope of one of its ancestors - e.g. the Window) you could still have such issues when an element was floating as a Window is a FocusScope and since it has no visual parent it would not cause the LostFocus/GotFocus when shifting keyboard focus between Windows (e.g. between the main window and a floating window).
Ok so if I understood well, I have no way to get the EditModeEnding event in time before hitting a button outside of my Focus Scope for instance. Relying on this event is not a good idea, isn't it?
tfresnea said:Ok so if I understood well, I have no way to get the EditModeEnding event in time before hitting a button outside of my Focus Scope for instance. Relying on this event is not a good idea, isn't it?
Actually, it seems to work with the LostKeyboardFocus event even when my ContentPane is floating and even if I click in another floating ContentPane or if I click in any other focus scope.
But now I have a trickier issue. I also have a Winforms control in one of my WPF ContentPane, and I can't get any events from this control before leaving it (it is an UltraWinGrid). I'm afraid in this specific case resetting the FocusedElement of the ContentPane to null will not work. Furthermore, this solution is not applicable to my application because I have a lot of buttons and ContentPane that are dynamically created in my application. So I should handle the FocusedElement resetting in all of them.
For my Winforms control, resetting the FocusedElement of the container ContentPane doesn't work. Do you have another idea of how I could handle this issue?
Ok. Thank you for your help.
I'm guessing that relates more to the Leave event of a WinForms control. I'm not sure how a WindowsFormsHost deals with when it triggers the Enter/Leave events. You may want to check the MS forums or perhaps review the WindowsFormsHost code in Reflector/ILSpy. From MS' documentation on WPF/WinForms integration it seems like they don't raise the Leave events in some scenarios.
As I said in my previous post, the main problem is fixed (the XamNumericEditor send a LostKeyboardFocus when I click in another focus scope).
The other issue I have is not related to a XamNumericEditor. It is related to a Winforms control (a UltraWinGrid) which is not a WPF component but which is hosted in a WPF control. For this specific component, I don't receive the CellChange event when I click outside of the Winforms control. I guess the issue is more related to Winforms integration in WPF and not only related to Infragistics components behavior, but if you have any advice...
I'm not sure what you mean. You originally asked about XamNumericEditor and EditModeEnding. It's a WPF control. I don't think the FocusedElement in WPF has any relation/affect on any HwndHosts that may happen to be within it.