How to handle the event when click on empty space? - wpf

I want to catch the event when a listview is left-clicked on an empty space - i.e. clicking on no item within the listview control.
I've search in the Events list of the listview but found none. How can I do this?
Please help!
[Edit]
What I want to do if I could catch this event: Deselect all items in the listview.

If you attach a handler to the MouseLeftButtonDown event on the ListView it will only fire when areas outside of a ListViewItem are clicked. Any clicks inside the items will be handled by the items themselves to drive the ListView's selection behavior.
You can make changes to the clickable areas by adjusting the Background ({x:Null} is not clickable, anything else is) and Margin of the ListViewItems by setting an ItemContainerStyle on the ListView. Also make sure that you are not using a null Background on the ListView itself (White is the default, Transparent works too).

ListBoxItem control handles clicks on ListBox. You should either:
use PreviewMouseDown event on ListBox
Add event handler in code via myListBox.AddHandler method
See How to Attach to MouseDown Event on ListBox for explanation and code examples.

I found if I had previously single clicked on an item in the listview (and thereby selecting it), then next double clicked on the empty space in the listview the undesired result of having the previously selected item being actioned as if it had been double clicked on (rather then the empty space). To get around this I used the following code (vb.net):
Private Sub ListView1_MouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles ListView1.MouseLeftButtonDown
ListView1.SelectedIndex = -1
End Sub
with this code in place double clicking on the empty space de-selects any previously selected items and has the desired effect of nothing appearing to happen for the user when they double click in an empty area.

Related

A strange effect when click some space in ListViewItem

My ListView's ItemTemplate is a very complex Grid, which has multiple rows and columns. The ListView's SelectionMode is Extended.
I met a very strange effect when I click my ListViewItem now. When I click some specific space, such as two gridrows' gap, the item isn't been selected, the ListView will scroll up, then the mouse pointed the next item, no item is selected. This effect only occurs when I click the last item in the view of the ListView.
The ListViewItem's IsSelected property is bound to my model's IsSelected property, and the binding mode is two way. My ItemsSource is ObservableCollection testList, I also have a ObservableCollection which contains the Items which are selected. If double click the item, a new window will be open which display the details of the selected model.
Sometimes, when I single click the space which I mentioned, it even has effect of double click, but an exception is thrown, because more than one item in the selected collection.
Anybody met the strange problem?

How to set focus to UserControl (make it selectable)?

I need to set focus to UserControl itself, not its child.
Otherwise I cannot implement insertion from the buffer. :(
Setting Focusable=True doesn't help.
Google and SO tells only how to set focus to UserControl child.
My control contains:
- Toolbar with several buttons bound to commands of the corresponding
VM
- TextBox which is the input for the filter
- DataGrid - list of items.
I need to bind Ctrl+V command to VM. But to handle this gesture UserControl must have focus within. When there are no items in the grid (VM's collection is empty) buttons are disabled and the only element which can get focus is TextBox. But it handles Ctrl+V in its own way and I don't want to change this behavior.
Thus, I need something to set focus to when I click the area of UserControl.
I believe UserControl is the best candidate for it.
But I don't know how to make it selectable.
The whole problem was in my misunderstanding of controls' behavior.
This SO question clearly shows it I believe.
Thus, setting UserControl.Focusable = true is not sufficient. To make it navigatable via keyboard IsTabStop must be true also. And to make UC selectable by mouse click we should call Focus() in mouse eventhandler. That's it.

How to Force Keyboard Focus on a DataGrid?

OLD TITLE: Inconsistent Actions in GridView SelectCell + Focus/Edit (GridView Reloaded Twice?)
I'm developing a GridView (bound by Data) set in the Content property of an Expander. When I open the Expander, I want the third column, first element to be selected and editable (but simply focused would be acceptable as well). When I tried to add this functionality to this action attached to Expanded, the grid never selected the first item for me. I did some troubleshooting, and strangely enough, I noticed something occurring. The Expander is part of a template within an ItemsCollection, so every time I add a new item to this ItemsCollection, an Expander is created on the screen (preset to IsExpanded = false). I set a Debug.WriteLine to the Expander_Expanded event, and the DataGrid.Loaded event as well, to let me know when the events were occurring. Here's the code for both of those events.
DataGrid.Loaded:
DataGrid dg = sender as DataGrid;
dg.Focus();
dg.CurrentCell = new DataGridCellInfo(dg.Items[0], dg.Columns[2]);
dg.BeginEdit();
Expander.Expanded:
Expander expander = sender as Expander;
DataGrid dg = expander.Content as DataGrid;
dg.Focus();
dg.CurrentCell = new DataGridCellInfo(dg.Items[0], dg.Columns[2]);
dg.BeginEdit();
When I create an item that contains the Expander and it is shown on the screen, the DataGrid.Loaded event fires. When the expander is expanded for the first time, the Expander.Expanded event fires, as well as the DataGrid.Loaded. Every other time after that, only the Expander.Expanded event fires.
The first time, the Cell at [0][2] is focused and in edit mode. Every other time, no cells are selected/focused. If there's no code in the DataGrid.Loaded event, then the Cell will not be focused or in edit mode regardless if it's the first time the Expander is opened or not. Expander.Expanded doesn't actually seem to do anything though, selection-wise. For the record, the bindings for DataGrid work perfectly, and no matter when I make changes (whether it's the first time the Expander opens or any times subsequently), the data updates properly. Can anyone explain this?
UPDATE:
After further investigation, it doesn't have as much to do with the GridLoaded event as it has to do with the Keyboard.Focus. The first time the DataGrid is loaded, the keyboard focus goes to the Cell (in the form of a TextBox) I ask it to. However, every time after that, the keyboard focus is still on the ToggleButton that opens the Expander itself. Trying to set Keyboard.Focus(dgCell) or Keyboard.Focus(dg) doesn't seem to do anything though, even if they're both focusable. I managed to validate this assumption by hitting the "enter" button when the DataGrid is opened. If the DataGrid has keyboard focus, enter will move to the next row. If the ToggleButton has the focus, it will collapse the Expander.
As noted before, the first time the Expander is opened, the DataGrid has Keyboard focus, but every other time, the focus always ends up with the button. Any suggestions?
I solved this by calling a later event. Using an Expander triggered a Keyboard.Focus switch to the button that toggles the expander after the Expanded operation happens (which nullified my focus switching work). So I used Expander_SizeChanged and added a check to make sure the event was triggered by a Expander opening and not by the window itself changing. Setting the focus here and selecting the Cell I wanted worked out.
Here's the code that made it work (expandSomething is a boolean triggered on the Expander_Expanded event listener).
Expander expander = sender as Expander;
if (expander.IsExpanded && expandSomething)
{
expandSomething = false;
DataGrid dg = expander.Content as DataGrid;
dg.Focus();
if (dg.SelectedCells.Count == 0)
{
dg.CurrentCell = new DataGridCellInfo(dg.Items[0], dg.Columns[2]);
dg.SelectedCells.Add(dg.CurrentCell);
}
else
{
dg.CurrentCell = dg.SelectedCells[0];
}
}

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

MouseOver event are missing alternately

I'm using a modified form of treeview, for the treeviewitem there is a template to show a textbox with a done button in a popup. I have used a static class to find if the mouseOver
(IInputElement currentPosition = Mouse.DirectlyOver;) event on any of the other treeview item to highlight them other than the one in Popup textbox. Items are added dynamically to this treeview. I'm using MVVM model here.
The problem is that when the first item is added and is in Popup textbox , the mouseover for the entire application is somehow turned off, which is not required. But on adding the second item and leaving it in the Popup textbox, the mouseover is switched on, i.e the treeviewitems get highlighted when mouseover occurs. this happens alternately....
What is capturing this mouseover event..??
try using snoop it can tell you what events are being raised, handled. and its a cool tool as well for the wpf man (or woman:)

Resources