WPF FocusManager interferring with focus - wpf

I have a grid, and in the grid I am setting my first element to be focused:
<Grid FocusManager.FocusedElement="{Binding ElementName=companyNameField}">
When the window opens, the correct control is focused.
But if I tab through the whole form, when the above focused field, should have the focus, there is no cursor evident anywhere on the window.
If I hit tab once more, it selects the control after the control that should be selected.
If I completely remove the focumanager attributes from my grid, I am correctly tabbing trough all my controls in the correct order.
Even stranger, if i leave in the focusmanager attributes and first click on the first text box and then focus through the entire form, then it selects my text box like any other control.
If you have any Ideas, I would love some help.
Thanks

I have had this happen to me, when I had some focus code in the code-behind fighting with the FocusManager. Mine was hidden in the Load of a nested UserControl, so I didn't notice it.

Related

How to set focus to a default child control unless the user clicks on a different child control?

I have a pretty simple use case but I cannot make it work right. I've got a ListView whose item template is a Note custom UserControl:
Each Note has a few simple controls as shown.
I want this to work so that if a row is selected programmatically or by clicking somewhere in it, it sets focus to the first textbox in that row. But if you click on a control in the row, it activates that control (i.e. lets you edit the Exhibit contents or click the Delete button).
If I don't do anything to set focus, clicking on the row highlights the row but doesn't set focus to any child control.
Within the Note control I have tried this:
protected override void OnGotKeyboardFocus(KeyboardFocusChangedEventArgs e)
{
base.OnGotKeyboardFocus(e);
text.Focus();
}
text is the name of that first TextBox, and it does set focus, but it also does this if I click directly on the Exhibit textbox or Delete button, making them unusable.
So, how can I enable the focusing that I want when the container control gets focus, UNLESS it got that focus via a click on a specific child control (which should then keep the focus)?
After a bit more searching, I found the correct way to implement this: at the root of the container control (the Note UserControl in my case), you can specify the name of the control to be the default focus control:
FocusManager.FocusedElement="{Binding ElementName=text}
Not sure how I missed that one.

ComboBoxItem gets highlighted when pressing Vertical scroll bar down repeat button in WPF

I have ComboBox which is data bounded to a collection.
I have customized the Vertical scroll bar of ScrollViewer, which will be in the center instead of normal right hand side of content presenter. It is working fine.
I have customized style for IsHighlighted trigger.
Issue: If I press the down repeat button it goes down and this is expected, but even if I disable the down repeat button, pressing on down repeat button highlights the item.
Any idea?
I don't fully understand the layout, a screen shot or a schematic drawing would help.
My guess is that you're using the "mouse over" and not the "mouse directly over" property to determine which element is under the mouse, so more than one element responds, but this is really a shot in the dark.
Actually the problem is in the ControlTemplate of ComboBox.
In ComboBox's control template I have used ScrollViewer and inside the ScrollViewer I used StackPanel. That is causing the issue.
Later I changed to ItemPresenter, now it is working fine.

wpf selecting controls inside a listbox

I have a textbox and some labels inside the data template of bounded listbox.
When I click on any label the whole item is highlighted in blue, but when I click directly on a different textbox the selection does not change.
Is there a way to make the selection of the listbox change even when a textbox is clicked?
thanks
This is what I've exactly asked few days ago, see post: "WPF: Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area"
basically there are few solutions, using code behind and XAML, but I've not verified latter approach yet
The reason is because the TextBox handles the click event in order to receive focus. There are a number of ways to handle this, including but not limited to:
stop the TextBox handling mouse events (which prevents the user from focussing it using the mouse)
use an eventhandler when the TextBox gains focus (or PreviewClick or similar), to select the parent ListItem

How to position a menubar above TextBox in Silverlight?

I want to add the menubar to TextBox control in Silverlight 4. (I will create a new reusable control.) The menubar will consists of a few image buttons. The idea is that it will normally stay hidden and will show up only when the user puts his/her mouse cursor to the TextBox area. If used in a multiline textbox, whole menubar can fit inside it, this should be easy. (I hope. :-))
But how to solve situation when TextBox is in single line mode? I'd like to put the menubar above the TextBox. But I don't have a clue how to do it. Can somebody help? I need to let all other controls in a form to stay in their positions, and only add my menubar above my textbox. (So the menubar will NOT hide the textbox. Instead, it will hide other controls residing right above the textbox.) It should work in all arrangements of form, like Grid, StackPanel, Canvas etc. In the fact it would be similar to a classic right-click context menu, but not modal. (Right-click context menu is modal, i.e. while it is shown you cannot use other controls, and it automatically hides when you click anywhere else. I want my menubar to stay visible as long as user heeps mouse cursor over the textbox or the menubar.)
Example: Coordinates of textbox are top=100,left=20,bottom=115,right=120. So my menubar's coordinates should be bottom=100,left=20, right & top are based on size of menubar.
If many textboxes will be used on a single page, each single one should have its own menubar. (Of course.)
You can create your own control (custom control or UserControl, whichever you like should work) which has the TextBox, and the visual for the menu bar.
If the TextBox is single-line, you could display the menu bar in a Popup which you position just above the TextBox whenever the mouse is over it.
If the TextBox is multi-line, you'd simply use a StackPanel or Grid or whatever to do layout like normal, if I am understanding what you want.
No coding required if you use this menu:
http://sl4popupmenu.codeplex.com
To achieve this behavior you will need to set its IsPinned property to true.

WPF UserControl swapping and preserving keyboard focus

I've got a WPF window where I have a column of buttons on the left side. On the right side I am show/hiding UserControls as the left hand buttons are clicked.
I have created the UserControls once and then just switch between them with the buttons. As I switch I would like to retain the keyboard focus where it was when that UserControl was last visible.
In other words, I click on button A and show UserControl A. If I move keyboard focus to a textbox in that UserControl then click button B, do some work, then click button A again I would like focus to be on the same textbox that I last used in UserControl A.
Any ideas on how I accomplish this?
Declare a dictionary with the key being one the left-side buttons and the value the currently focused control. When a button is clicked, get the currently focused element and set it in the dictionary (with the key being the previously clicked button). Change the displayed UserControl and read the dictionary with the key being the just-clicked button. If there is a control for this entry, set the focus to it.
Use FocusManager.FocusedElement to know which control has the focus (actually an IInputElement, which should be the type of the dictionary value) and FocusManager.SetFocusedElement to put the focus back (or call Focus() on the control).

Resources