in one of my WinRT App's Views, I'd like the BottomAppBar to be displayed when a TextBoxloses focus. I played with the GotFocus and LostFocus events with which I can manually change the IsOpen property of the BottomAppBar accordingly.
Problem is, when the BottomAppBar is open, if the user clicks on the TextBox, the BottomAppBar is closed (standard AppBar behavior), but the TextBox is not focused (even though the user clicked right on it). The user needs to focus the TextBox again to be able to type something again.
If I hook myself up in the Closed event of the BottomAppBar to try and programmatically set the focus to the TextBox, it shortly becomes focused, but loses the focus right away and instead, its ScrollViewer gets focused.
Any idea why the TextBox loses the focus the second time?
Any idea how I can do what I'm trying to achieve?
Thanks!
Perhaps your BottomAppBar gets closed when you tap out of it and on the ScrollViewer and setting the focus to your TextBox gets overriden by the focus being set to the ScrollViewer right after that. If you only ever want focus to be on the TextBox - you could disable focus from ScrollViewer. You could also try to set the focus after a delay (either with await Task.Delay(50); or with await Dispatcher.RunAsync(() => /*set focus) so it might get set after the ScrollViewer gets focus or handle GotFocus on the ScrollViewer and set the focus back to the TextBox when you want it to keep the focus. Finally make sure that the TextBox can get focus at all.
Related
I have a WinForms UserControl that accepts keyboard input, and had a Scrollbar for scrolling, and everything was fine. Recently I swapped the Scrollbar control out for a custom scrollbar (also a UserControl), and now after clicking the custom scrollbar, my custom control loses focus and the only way to get it back is to click a different focusable control (like a TextBox) and then click back in my UserControl. If the scrollbar has focus and I click inside my UserControl to give it focus, I notice the LostFocus event is raised and the scrollbar keeps the focus
I tried setting the UserControl's Selectable control style to true, it didn't help.
Any idea why it would behave this way?
I figured it out. I used
SetStyle(ControlStyles.ContainerControl, false);
In the UserControl's constructor. Because it had the ContainerControl flag set by default, it would preferentially give the focus to a child control.
I have a TextBox in WinRT which got the focus (Pointer focus) on load in a view in WinRT. I want to disable this and change the focus to unfocused at load. When I am in constructor of the view, the TextBox is Unfocused, but when I reach the event Loaded, the TextBox got, I don't know why, automatically, the focus at Pointer.
No instruction are done to put the focus on this control. I don't understand why it got the focus.
I try to change TabIndex, no success, the control got the focus again. When I try to put manually the focus on unfocused, I have an exception: "Value does not fall within the expected range.". I don't understand why I have this exception. I have only one control with the name that I gave to him.
Thank's for reply.
Solution 1:
You can set the textbox TabStop property to false.
Solution 2:
Set another controls TabIndex to a lower value than the TabIndex value of the texbox (the control you wish to recieve focus on startup).
I've a listview and a textBLOCK inside it. I want to collapse the visibility of listview when I tap anywhere else on my screen. I tried to do this using LostFocus Event for my listview but it is fired ONLY when I selected an item. Why does it behave like that?
Thanks in advance!
The only way to do this as of right now, based on this seems to be to hook into the touch input directly from the CoreWindow and then each time compare the point on the touch input with the bounds of the ListView relative to the CoreWindow.
The WinRTXamlToolkit may assist in this by adding such extensions as GetBoundingRect to FrameworkElements.
I used the IsTabStop Property. Actually i have a user control in in which i'm having this Listview. I did the following steps:
I set the IsTabStop property of UserControl to "True".
Then I set the focus to the textblock when it is tapped by doing:
this.Focus(FocusState.Pointer);
Then created the LostFocus Event of my UserControl and Collapsed the visibility of ListView.
Try using TextBlock's Leave or LostFocus event because if you focus the TextBlock, the ListView losts focus but if you focus TextBlock when you focused something out of ListView, the ListView never becomes focus.
I am trying to give a specific control keyboard focus during a sequence of presentation changes. Say a control was in a visibility state of hidden. In a function call I can change its visibility to visible, set the keyboard focus on it with no problems. However if the control was set to a visibility of collapsed and I try to set the visibility back to visible then set focus on it, it does not take.
Is there a process I can use to allow the control to move from a collapsed state, to a visible state and take keyboard focus?
It's probably a timing issue where the control you set to visible is not ready to accept focus yet. This can happen fairly frequently when dealing with dynamic UI changes.
You should still be able to focus your control but you'll need to queue the focus action until after the control has been properly initialized. Simply using a BeginInvoke call like shown below, should do the trick.
Dispatcher.BeginInvoke(() =>
{
//Set focus here
});
Its not a matter of the control having been initialized. This again this is an issue of transitioning visual states.
Just to close loop on this, the problem is solved by calling the control.UpdateLayout()
I've built several user controls in WPF and they all get added to a canvas. I want to add a behaviour that keep tracks of the currently selected usercontrol. A usercontrol should be selected when:
The mouse clicks on it;
when it recieve focus;
when either of the two above happens to a subcontrol of the usercontrol.
Is there any way to handle this purely by using the focus mechanism of WPF or will I need to take care of this myself with assistance of the focus classes?
I've read up upon the new way of handling focus in WPF, and the problem I'm facing is that the keyboard focus determines what the currently selected object is, but some parts of the my control can't recieve keyboard focus so even though these parts are clicked, the usercontrol doesn't recieve focus.
I'm looking for advice on how to implement this feature and how much I could/should rely on the focus mecanisms. Ultimatively I wouldn't mind if only a single object could be selected, but if it's easily extendable to multi-select then I wouldn't mind this either.
Just to clarify, I know I could build this manually by handling a lot of events and keeping track of states, but I was just hoping an easier approach was available.
Combine UIElement.IsKeyboardFocusWithin with a PreviewMouseDown handler:
When PreviewMouseDown is called, set a flag and schedule callback using Dispatcher.BeginInvoke at DispatcherPriority.Input to set focus to the UserControl if the flag is still set.
Set a handler for UIElement.IsKeyboardFocusWithin property changes in your UserControl. When the handler fires, clear the flag.
The idea here is that if you click anywhere on the UserControl and keyboard focus does not result in the focus being moved into the UserControl, force it into the UserControl.
FYI, here's roughtly what step 1 looks like in code:
public override OnPreviewMouseDown(MouseButtonEventArgs e)
{
_mouseClickedButNoFocus = true;
Dispatcher.BeginInvoke(DispatcherPriority.Input, new Action(() =>
{
if(_mouseClickedButNoFocus)
Focus();
});
}
You could use the UIElement.IsKeyboardFocusWithin property, which is true when the UIElement or one of its children has keyboard focus. It is a dependency property, so you can easily use it for a trigger in a style
You can set the logical focus to the control when any of the child gets the keyboard focus using FocusManager.IsFocusScope="True". Setting the keyboard focus to the control or trying to do will eat the keys for the child controls.
You can use UIElement.IsKeyboardFocusWithin to set the focus of the control if any of the children has focus.
You can read this article which I think describes the difference between Logical and Keyboard focus quite well:
http://www.pluralsight.com/community/blogs/eburke/archive/2009/03/18/why-is-focus-in-wpf-so-tricky-managing-and-understanding-focus-in-a-wpf-application.aspx