Where does keyboard focus go when the focused WPF element is removed? - c#

I have a custom TreeView-like control in a panel in my application. As you click on items they receive keyboard focus.
It's possible to select a set of items in the tree and cut or remove them by pressing Ctrl-X or Delete. When the tree items are removed, keyboard focus reverts to the main window.
This leads to trouble - if I Undo my cut, I get my elements back but keyboard focus is no longer on my panel so I can't (for example) go cut-undo-cut-undo-cut.
What determines where the keyboard focus moves when an element is removed? I quickly tried making my panel have IsFocusScope="True" but that didn't seem to have any effect, and the Focus Overview doesn't mention how to control where focus goes when an element is removed.

I guess your best bet might be setting the focus manually after undo/redo (maybe encapsulating this in a behavior which listens to the events FocusManager sends?)
See also Set focus on textbox in WPF

Related

scroll between grid buttons using mouse wheel in UWP

I have a UWP app in which I have several buttons. Once the app starts to run, I set the focus in my code to the first button using a code like this:
firstButton.Focus(FocusState.Programmatic);
After this point, what I am interested in is that once the user use the mouse wheel, the UWP app automatically scroll to second, third, fourth, ... button(exactly like when we use tab key on keyboard to move between buttons).
However, when I use mouse wheel, nothing happens in the app.
I should also say that in firstbutton xaml, I use pointerwheelchanged event listener to change the focus to second button. However, this event handler does not work with mouse wheel UNTIL I MOVE THE MOUSE CURSOR INSIDE THE AREA OF FIRST BUTTON. What I am interested in is that this scrolling using mouse wheel becomes automatic exactly like the tab key of keyboard.
Any suggestions?
Place the event on the container control (like the Grid). If it's a control that already processes the event, use the AddHandler method with handledEventsToo set to true.

uwp xbox creator program - button focus, how to set in code-behind?

I am starting to get my feet wet in the Xbox UWP Creator Program. I'm coming from Windows Phone 7 & 8.
I have not been able to figure out how to set the Button Focus to my desired buttons via code behind or StoryBoard animation.
I have a single page app that is using 13 buttons in total, but not all buttons are on screen at the same time. The buttons are animated onto the screen in pairs (Yes - No) using Storyboards.
The First Button has correct focus when displayed.
The next two buttons are displayed after first button is pressed and deactivated with Visibility.Collapsed. The Right most button is displayed with Focus when these buttons are set with Visibility.Visible. I need the left button to have focus.
The second pair of Yes-No buttons are displayed when first pair is hidden with Visibility.Collapsed. When these two buttons are displayed the focus rectangle is not visible. It appears when Left Stick or D-Pad is moved. I need the left most button to have focus.
All the remaining pairs display without focus set, as above.
I have been reading docs here:
FocusManager Class
Focus navigation...
Designing for Xbox and TV
But frankly, I'm a bit stumped on how to set, or reset the focus on UI elements using either in code-behind or in a StoryBoard.
I need to use the separate pairs of buttons as opposed to just using two buttons (1 yes, 1 no and using a case statement) for visual reasons. So popping these buttons on and off is messing up with the built-in visual state focus manager.
Any pushes in the correct direction would be most welcomed.
Thank you,
-David
E.g., set focus to the button called MyButton from code-behind:
MyButton.Focus(FocusState.Programmatic);
You may also find the following properties of UI elements useful:
TabIndex to change the order in which elements get focus.
AllowFocusOnInteraction to keep a UI element from getting focus when it's interacted with.

Make Gridview horizontally scrolling when pressing arrow keys in Windows Store app

My gridview is horizontally scrollable using mouse wheel by default. I need a feature which will enable the same thing using right and left arrow keys.
First of all I tried to handle KeyDown event for Gridview, it didn't work. Then I found this event Window.Current.Dispatcher.AcceleratorKeyActivated, and it captured all the keys I pressed. So the next step was what to do with scrollviewer. I tried to put my gridview in Scrollviewer in xaml, but it wasnt what I wanted. And the scrollviewer which is by default in gridview, doesn't have any functions which will enable the same thing which is done by mouse wheel. I wanted to perform PointerWheelChanged, but I didnt know what to give as eventargs. Please help me, i'm new to WInRT
P.S. I need this scrolling when the(parent) window has got the focus, but gridviewItem has not. Because if I select an item, i can switch between selected items using arrow keys. (I will not have item selection in my final app).

Container Control is losing focus

I'm trying to create User Control which will work like a rich button.
It's supposed to have an effect on hover - I turn border on on MouseEnter and off again at MouseLeave.
BUT, when I hover over label in my control, it fires Control.MouseLeave.
Is there any way to prevent this?
First, focus Enter and Leave are different events than MouseEnter and MouseLeave. Focus deals with keyboard input. MouseEnter and MouseLeave deals with where the mouse is right now.
Entering a nested control fires MouseLeave on the parent control. You can capture the mouse by setting the control's Capture property to true, but you may find that doesn't behave like you'd expect.
You might look at my post here. I had the same issue with nested controls. I opted to create a .NET equivalent of a mouse hook by calling Application.AddMessageFilter.
Another option would be to remove the inner label control and draw the text manually it in the button's OnPaint.

In a WPF ListView how can I prevent auto scrolling?

I have a WPF ListView which currently scrolls everytime I click on an item which is only partially visible. How can I keep the control from scrolling that item into view (instead simply selecting the partially visible one)? This behavior is very annoying when doing a drag from this control.
Thanks.
Added: I am looking for a solution to keep the control itself from scrolling when contents are clicked that the control believes are not fully visible. Often this is by a few pixels and the scroll is not necessary.
The items scroll into view because the default behavior on list item click is to call BringIntoView(). You can add an event handler for the RequestBringIntoView event and catch it before it bubbles up from the ListViewItems to the ScrollViewer. In your handler, check the bounds of the sender against the visible region and if you decide that you don't need to scroll, set the event's Handled flag to true.
Since I'm currently on the road, I cannot try this, but have you tried playing around with CanContentScroll, and/or wrapping the scrollable content into a Panel, as suggested by the ScrollViewer Overview on MSDN?
In the worst case, you might want to replace the ListView's ItemsPanel by a hacked ScrollViewer with a "fuzz" factor, e.g. by capturing the RequestBringIntoView event.
have you tried this approach?
just as an addon, I don't know how much you know about the subject, but here is a good place to read more about it.
added:
I just found that you can prevent the mouse wheel of scrolling as well.

Categories