Issue with WPF Keyboard Focus in itemControl - c#

I have a WPF window which is divided into two portions. One portion contains itemscontrol which generate textboxes depedning on observable collection binding in ViewModel.
The other portion of window has a content control which loads a usercontrol using datatemplate and viewmodel binding. Also that user control has buttons on it.
The problem is i want to keep keyboard focus within the textboxes in itemscontrol at all times and don't want the keyboard focus to shift to the usercontrol button even if they are pressed.
Currently the keyboardfocus shift from the text boxes to the usercontrol buttons when the button is pressed.
Is there a way i can keep keyboard focus in textbox in itemscontrol.

Setting Focusable="false" on the buttons should be sufficient.

I am just shooting in the dark, but here is something you can try: in your user control set Focusable to false and FocusManager.IsFocusScope attached property to true.

What about changing the focus back to the textbox in the button click event?

Related

How to make a Textbox in a UserControl not receive focus on Load?

I have a XAML page where, upon tapping an "Add" button, I create a UserControl and add it to my Container. This UserControl has a Rectangle, a TextBox and a few buttons. My problem is that on the first click of the "Add" button, the focus is given to the Textbox. Clicking on the "Add" button repeatedly creates more UserControl instances, but focus remains on TextBox1. If I now click on Textbox5, that box gets focus, but as soon as I click outside, focus returns to Textbox1. I would like focus to be given to textboxes, only on click.
I have tried setting IsTabStop = true in XAML, and intercepting the tapped event and setting it to false, but that doesn't have a noticeable effect.
What worked perfectly is setting the TextBox's TabIndex = 2, and creating another button before it, and setting it's TabIndex = 1. But I lose this benefit when I set the Button's Visibility = Collapsed. The TextBox is the left most control, so it must have the lowest TabIndex (well technically, there is a Rectangle to the left of the TextBox, but since a Rectangle is not a Control, it cannot have a TabIndex).
How can I fix this?
I believe what is happening is when you are adding more controls, it is resetting the current tab stop index. So every time you add a control, the TextBox of TabStop 1 is being focused on.
What you can do, is after you add the control, call Focus() on the control that you want to be focused.

WPF Prevent button from taking focus from any other control

I have an "On screen keypad" with some up/down/left/right/select buttons.
The select button is effectively a click and the arrow keys fire the associated up/down/left/right key.
The problem is that when selecting a combo box, I can't press the down/up buttons to navigate the items in the list. It is because the combo box auto closes when loosing focus. I can see similar problems happening with other controls, so I would like to see if there is a way to do the following.
For certain buttons (up/down/etc), when clicked, fire the click event, but don't take focus from w/e currently has the focus. This would allow the combox dropdown to stay open while pressing up/down to navigate through the items.
I have tried to set Focusable=False on the navigation buttons but the focus is still taken away from the combo box and the dropdown closes.
Any ideas/suggestions?
Thanks in advance
This isn't happening because of anything your Buttons are doing so changing their focus state won't make any difference. ComboBoxes close when you click anywhere outside of them, including empty space, non-interactive controls, other windows...

Scrolling focus with combo box Tab control in C# winapplication

After you click outside the drop down, it seems the focus is still on the drop down; scrolling, you select (without knowing) another value.
I want to lose the focus while clicking on the tab control or any location of the page.
As Otiel commented, click outside ComboBox won't make it lose the focus.
You can handle the Click event for the container (eg. a Panel) of your ComboBox and set the Focus to other control by using Control.Focus Method.

How does ToolTip show Popup on a control?

When set a TooltipText on a control, and the tooltip text will be shown when user move mouse on the control. Tooltip will detect MouseEnter or MouseLeave or anything for this purpose?
I want to know how does a Tooltip show Popup on a control?
Assume that I have a user control with name 'UserControlX'. On UserControlX, I put a button and set Dock property to Fill. I add a UserControlX on Form1, add a ToolTip and set a text to this usercontrol. ToolTip will be not shown when user move mouse on control because user is moving mouse on usercontrol's button, not usercontrol, so the tooltip will never show.
Please help me how to solve this problem so that when move mouse on UserControlX, the tooltip will be shown. Thanks.
I believe the tooltip displaying on mouse-over is defined within the control's default template. if you view the default template you will likely see a reference to a tooltip in there. If you go further and view the Tooltip's default template, you will see how it is composed as well as what events it listens to.
To answer your question, you could do what Adrian suggests and put the tooltip on the button as well as the parent control.
If you have time to mess around a bit, you COULD try to see if there is a tooltip displayed event or something to that effect for the button, and then simply invoke the parent control's tooltip manually. It could be considered a hack, but worth a try, maybe.

How to focus an image in Silverlight?

How to focus an image in Silverlight?
For example: imagine that when my page loads, It will focus on a texbox, so I don't have to click in there to control. I want the same with image control.
There is no way for an Image to acquire input focus directly.
Input focus is supported only on Controls and its descendants which have the properties IsEnabled and IsTabStop set to true. Image is derived from FrameworkElement, not from Control, so it's not technically a control.
You could create a UserControl that contains the image and then call .Focus() on the container control.
It sounds like you want to make a button of the image and than set the button with the default focus.
Make a button with an image
Set button with focus

Categories

Resources