Determine who has focus in WPF Window - wpf

We are using WPF and have a window derived from a DockingLibrary. This window has a grid with multiple items in it, one being a WPF datagrid. We are using the M-V-VM pattern. When this windown is created and shown, none of the rows in this datagrid are selected. We can set the row to display as highlighted by doing something like:
SharedWindow.ShipmentWin.shipmentDataGrid.SelectedIndex = 0;
This causes the first row in the datagrid to be shown as highlighted. But, and isn't there always one of these, this row is not Selected nor does it have Focus. I tried setting IsSelected and Focus on this row as in:
SharedWindow.ShipmentWin.ShipVM.IsSelected = true;
bool focused = SharedWindow.ShipmentWin.shipmentDataGrid.Focus();
Am I going about this all wrong and is there some other way to Select the first row in the datagrid and set focus to it? Typically, when a datagrid is created, no row is selected until the user mouse clicks on the desired row.
Any thoughts would be greatly appreciated.
thanks!

Have a look at the FocusManager. It allows you the set the focus to another UI element via the SetFocusedElement method. Additionally, it allows you to determine the currently focused element in your application which is can get handy to debug focus issues. Snoop can be useful, too. It shows the currently focused element in the bottom status bar.
If you use the DataGrid from the WPF Toolkit, be prepared to find some bugs in relation to focus handling. Some have been addressed recently.

It's also worth understanding the difference between logical focus and keyboard focus, which are quite different animals. The .Focus() method sometimes only sets logical focus - which probably isn't what you want.
The documentation for the Focus method tells you that it will return true if the keyboard focus was set, and false otherwise.

Related

XamDataGrid XamTabControl WPF MVVM Peculiar Behavior

I'm facing a problem in implementing the following scenario:
When the user selects a record in a XamDataGrid, the control flows into another tab showing the details of the selected record.
The way I have done this is:
At the selection change of the XamDataGrid, I'm sending index of the tab to be activated using MVVM Light Messenger.
Setting the property bound to the Seledted Index of the Tab Control to that number.
Everything works smooth till here. Afetr that if I go back to the tab containing the grid, somehow the control comes back to tab which was activated.
As if, some intrinsic selection of cells in happening in the grid. Does Hover also affects the selection of the xamdatagrid?
I'm new to Infragistics and kind of confused. If some one can help.
All code are in View Model files and no code in the code behind of xaml file.
Cheers,
Anshuman
I played around a lot.
But the only way to get this wqorking was to disable all hover animations and effects.
Now it works as expected!

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 Disable Tab functionality inside a grid in WPF

My requirement is rather a tricky one (it seems to me).
I will explain the scenario.
I have a DataGrid. In the DataGrid, I have two columns in which I have a grid in every cell of these two columns, inside of which, there are two comboboxes - the purpose being to switch the visibility based on some conditions.
When we select a value in the combobox, the combobox itself, plus some other controls in some other columns will get disabled (requirement of the pjt). Now there is also, another requirement like, Tab should not be allowed in the cell which has disabled controls - say disabled combobox.
We are setting the controls as Enabled or Disabled based on a selected value from the combobox. So, since we are applying the disabling property on the control level, and the IsTabStop property is on the cell level, I am not able to restrict Tabbing in the cells having disabled control.
Any thoughts?
Don't use a DataGrid.
DataGrid's are awesome for read-only stuff, but they seem to suck for doing any kind of interesting editing. After well over a week of fighting with it, I've given up on it.
Right now I'm looking for a replacement, which may end up just being a scrollable stack panel with manually added controls.
WPF: Is there a "ListBox" without the ability to select items?

Silverlight: Individual visibility of a RowDetailsTemplate for each data row

I'm trying to achive displaying the RowDetailsTemplate of a Silverlight DataGrid depending on a bool Property, bound to a CheckBox Control's IsChecked Property. Insinde of my RowDetailsTemplate there is a single custom UserControl, containing further Controls.
Since the DataGrid only allows a global setting (RowDetailsVisibilityMode) Some code-behind is needed. I've implemented a solution based on Rorys Reply (and using a behaviour-technique) which actually works.
Unfortunately, The DataGrid doesn't remember the individually shown or hidden Rows on sorting. The checkbox remains selected, but the Row collapses. Further, no event like "OnAfterSort" or something similar seems to exist, where i could "Refresh" the visibility settings in a loop.
Another idea was to bind the Visibility of my custom Details-UserControl to the CheckBox bound value. This actually works (when settings RowDetailsVisibilityMode to "Visible"), but I'm not able to get rid of this weird behaviour: When the CheckBox is checked, the Detail Template expands and the detail UserControl appears. Nice. When the CheckBox is unchecked again, the UserControl disappears (Visibility is set to Collapsed) but the Row doesn't collapse and the blank space remains (as it would be set to Hidden not Collapsed).
Do you have any ideas?
I hope it's ok I didn't post any code samples, the implementation is pretty easy and I believe that the problem doesn't actually lie in a coding mistake i made. You can setup a simple DataGrid quickly like in this perfect MSDN Example. Starting from here, it's easy to test both described behaviours!
Really big thanks in advance,
- Thomas

WPF Datagrid deselects row when control is disabled

I have a program in which a user selects a row in a Datagrid and then clicks a "Start Recording" button. While "recording" is happening, they are not allowed to change the value selected in the datagrid, so I set IsEnabled to false. However, when the datagrid is set to be disabled, it deselects the selected row, which screws up any bindings I have to the datagrid's SelectedItem propery.
Is there any way to keep the datagrid row selected even though the control is disabled?
Edit: This does not happen in Windows Vista, but it does in Windows 7.
If you really want to 'record' actions but still keep visuals and interactions looking the same, why don't you just add a check to the event fired on selection to ensure that recording is not taking place and set e.Handled = true.
Alternatively you could set IsHitTestVisible = false and prevent them from taking actions in the control instead of disabling it outright.
Hope that helps.
Sorry I know this post is a little old, but I couldn't find another solution to this anywhere else.
It doesn't seem to be related to Vista\7, but to the Feb. release of the Toolkit.
You can set IsHitTestVisible = false as Jeff Wain suggests, but as Mike noted it doesn't appear different. Also, It doesn't disable Keyboard input.
My solution is to put the DataGrid in a Grid within the same row and column as a semitransparent gray rectangle (This will make them on top of one another). You have to put the rectangle in the Grid second to make sure it is on top of the DataGrid. When I want to 'disable' it I make the rectangle visible. This will make the list look dimmed and disable mouse inputs, but it still doesn't disable keyboard input.
To disable the keyboard I have to intercept 'PreviewKeyDown' and set e.Handdled = true. This will not allow anything else to be selected but will still do some interesting things when you tab to it (like tab no longer working). Perhaps setting it to not be a tab stop and not focusable will also fix this, but disabling selection is all I really care about.
IsHitTestVisible=false disables mouse inputs.
For disabling keyboard inputs set Focusable=false.
Both should be set via a style in ElementStyle and/or ElementEditingStyle for builtin datagrid columns inorder for the child control (textbox, checkbox, etc) to not accept input.
You'll most likely have to use a trigger in the style and bind it to some IsRecording value.
Also you could, in the same style, change the appearnce of the "disabled" controls by setting their Opacity=0.4, this gives somewhat of a disabled feel to them.

Resources