WPF giving keyboard focus to user control - wpf

I have a window that displays a number of user controls. One of these needs to expose some keyboard shortcuts, which I've defined like so:-
<UserControl.InputBindings>
<KeyBinding Key="N"
Modifiers="Control"
Command="{Binding AddCommand}" />
..etc..
</UserControl.InputBindings>
The user control itself has a variety of child controls - TextBoxes, a DataGrid, buttons, etc. The keyboard shortcuts will be used to manipulate items in the DataGrid.
When I click on the UC, the keyboard shortcuts may or may not work, depending on where I've clicked. E.g. they won't work after clicking on the DataGrid, but will work after clicking on a TextBox or Button.
I'd assumed that clicking anywhere on the UC would have naturally given it focus. Is it something to do with keyboard focus and mouse focus being two different things? Not really had to handle this sort of thing before now.
How do I get the keyboard shortcuts to work - either when the UC first appears, or whenever the user clicks anywhere on it?

Related

WPF MenuItem usability by mouse

WPF MenuItem is only selected when user hovers mouse exactly over the text of MenuItem.
If user clicks now then menu will be expanded, good:
This does not happen when mouse is not over the text.
If user clicks in this situation then nothing happens (you can see background of MenuItem is darkened, i.e. mouse is not over the element):
This is quite incovenient, uses complained, I even set bold font to increase area where user clicks succeed to open submenu.
Code is very simple:
<MenuItem x:Name="MainMenuItem" Header="Menu" ToolTipService.HasDropShadow="True"
Foreground="{StaticResource BasicBritnessForeground}" FontWeight="Bold">
How to fix this?

Window GotFocus event not firing

I couldnt get the GotFocus() event fired in the WPF window by clicking. But if I click any controls, it will be fired.
My requirement is as follows: I have a hostwindow which has a viewport with two datagrids which can host user controls. I do animations like flip and fade on this user controls. I am using a transparent background(glass effect) for the controls. But when one user control get on top of the other, both of them overlap. So I want to make the top one opaque when the user click on one user control.
Try using the Activated event, as GotFocus is intended to be used only with controls.

TextBox Cursor is NOT blinking

I have a WPF datagrid (4.0) with a custom column (derived from DataGridTextColumn).
In GenerateEditingElement I create a custom textbox control (with an additional button) and like to set the cursor into it so that the user can directly start editing.
The closest I get is that the caret is shown but is not blinking and I need an additional click to start editing.
All other stuff (binding, ...) is working nicely
Any ideas?
Since the caret is shown, but not blinking, then I am guessing your control has Logical Focus, but not Keyboard Focus.
How are you setting the control as Focused?
myControl.Focus(); will give the control logical focus, but it won't respond to keyboard events because it doesn't have Keyboard focus. To give an element KeyboardFocus, use
Keyboard.Focus(myControl);
This is because WPF allows you to define multiple Focus Scopes, and each scope can have it's own focused element, however only one control in the entire application can have Keyboard Focus

WPF: How to change button content (text) with a double click at runtime

I have a button in WPF, I want to change the text when I double click on it, that is I want the cursor to appear and type the text that is to be shown as the content (similar behavior as when pressing F2 on a desktop shortcut).
I guess I could detect a double click and then show a textbox with a transparent background, that will get me the cursor, type the text in this new textbox, set it to the buttons content and delete the textbox, but that doesn't seem the right way to do it.
I guess what I had in mind, is that I am developing a diagramming tool using shapes. Since shape doesn't derive from ContentControl I cannot put a text box inside it, and I want to simulate this behavior. I was thinking of making a custom control but that might be too much work for this, and am not quite familiar with this topic yet. I guess another approach would be to use an adorner (maybe a border) and since it derives from contentcontrol I can do the same thing as joe suggested. any ideas?
Another thing I could do would be to put the shape in a grid, and then put the textbox on top of the shape, but I am not sure how would that be as a design principle, and also I don't know if the hit testing would only be on the shape or the grid.
Since this is WPF, you can put a TextBox inside your Button with no trouble. If you don't want the textbox to have a border and white background -- i.e., if you want it to look like you're just typing directly into the button -- then you could remove them by setting BorderWidth to 0 and Background to Transparent.
What you probably want to do is have your Button's Content be a Grid that contains both the normal content (probably a TextBlock) and the TextBox, with the TextBox initially hidden (Visibility = Collapsed). Then when you get the double-click event, you would hide the TextBlock and show the TextBox.
<Button>
<Grid>
<TextBlock Name="buttonText">Double-click to rename me</TextBlock>
<TextBox Name="buttonEdit" Visibility="Collapsed" MinWidth="100"/>
</Grid>
</Button>

WPF FocusManager interferring with focus

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.

Resources