ListView LostFocus Event isn't working fine - c#

I've a listview and a textBLOCK inside it. I want to collapse the visibility of listview when I tap anywhere else on my screen. I tried to do this using LostFocus Event for my listview but it is fired ONLY when I selected an item. Why does it behave like that?
Thanks in advance!

The only way to do this as of right now, based on this seems to be to hook into the touch input directly from the CoreWindow and then each time compare the point on the touch input with the bounds of the ListView relative to the CoreWindow.
The WinRTXamlToolkit may assist in this by adding such extensions as GetBoundingRect to FrameworkElements.

I used the IsTabStop Property. Actually i have a user control in in which i'm having this Listview. I did the following steps:
I set the IsTabStop property of UserControl to "True".
Then I set the focus to the textblock when it is tapped by doing:
this.Focus(FocusState.Pointer);
Then created the LostFocus Event of my UserControl and Collapsed the visibility of ListView.

Try using TextBlock's Leave or LostFocus event because if you focus the TextBlock, the ListView losts focus but if you focus TextBlock when you focused something out of ListView, the ListView never becomes focus.

Related

WinForms UserControl can't Focus properly

I have a WinForms UserControl that accepts keyboard input, and had a Scrollbar for scrolling, and everything was fine. Recently I swapped the Scrollbar control out for a custom scrollbar (also a UserControl), and now after clicking the custom scrollbar, my custom control loses focus and the only way to get it back is to click a different focusable control (like a TextBox) and then click back in my UserControl. If the scrollbar has focus and I click inside my UserControl to give it focus, I notice the LostFocus event is raised and the scrollbar keeps the focus
I tried setting the UserControl's Selectable control style to true, it didn't help.
Any idea why it would behave this way?
I figured it out. I used
SetStyle(ControlStyles.ContainerControl, false);
In the UserControl's constructor. Because it had the ContainerControl flag set by default, it would preferentially give the focus to a child control.

When to focus first element in ItemsControl after data-binding in WPF

I have an ItemsControl control which is data-bound to a list. In the ItemsControl is a DataTemplate which displays all data-bound items as buttons.
Now I want the first button to receive focus.
When do set the focus? Doing this in the ContentRendered and DataContextChanged events don't work the controls don't seem to be rendered at that point.
Bonus question: what's the best way to look up such a button on my window?
I'm guessing the DataContextChanged event doesn't work because the DataContext actually changes before the ObservableCollection you are binding to has any content.
I haven't had a chance to test it, but just a thought, perhaps you could try setting the focus in the TargetUpdated event handler on your binding to your ItemsSource.
The only issue with this is that if the collection continues to update then the focus will continually return to the first button, but this is something you can handle with a simple flag.

WPF: How do I handle mouse click on a Listview inside of a Button?

I have a listview for displaying a table inside of a button. I have a click event assigned to the button, but when the user clicks on the list view, a row is selected on the list view and the mouse click is never bubbled up to the button.
I'm stuck at this point, and need a way to solve this. All the examples I've seen online are for placing a button inside the listview. How do I make this work?
Set the IsHitTestVisible property to false on your ListView. Since it is a control that normally processes click events, doing this will have it ignore them.
If you don't want the items to be selectable in the first place, you could make the List View unable to receive Focus. Just set the Focusable property to false.
With the List View unable to receive focus, then I would expect the mouse event to bubble up to the button.

Hold focus on textbox that do something in the textchanged event

I have a problem:
I handled textchanged event of a textbox. When the event fires I do something in the UI (like add a row in a listbox) and the textbox lost the KeyboardFocus.
How can I hold the KeyboardFocus on that textbox?
Thanks
Write the following
this.MyTextBox.Focus()
at the end of your handler.
or make your UIElement.Focusable = false. it should help.
You can set the focus back on the textbox in the code-behind where you handle the event using textBoxName.Focus();
Focus can be set in the XAML using
FocusManager.FocusedElement="{Binding ElementName=textBoxName}"
but if the focus is lost when you add a new element then you may need to use code-behind to reset it.

Combobox selection control not coming out

I'm working with winforms comboboxes. When I select any item in combobox I've raised an event. But control can not come outside. When I scroll the mouse the selected event changing every time.
how to prevent this?
The mouse scrolling actually does change the item selection in the combobox and hence your event is fired, you don't want your event handling code on mouse scroll?
If the problem is that you cannot use the mouse wheel to open drop-down portion of the combobox, then you can use ComboBox.DroppedDown property.

Categories