Detecting loss of keyboard focus in a Silverlight application - silverlight

I have a Silverlight game controlled by the keyboard, and I want it to go into pause when it loses the keyboard focus (e.g. the user clicks on another part of the hosting webpage, or moves to another browser tab).
I used to do this in Silverlight 1.1 by subscribing to the LostFocus event on my RootVisual UserControl, but in the last two versions of Silverlight, I have found this event seems to fire unexpectedly shortly after clicking a button in my application (in Silverlight 2 it fired once, in Silverlight 3 twice!).
Is there a way in javascript on the hosting page, or within Silverlight to detect loss of focus more reliably?

I finally found a solution to this problem. The RoutedEventArgs property on the LostFocus event has an OriginalSource property which allows me to ignore any LostFocus events that come from children of the RootVisual.
void Page_LostFocus(object sender, RoutedEventArgs e)
{
if (e.OriginalSource == this)
{
Pause();
}
}

Related

parent object mouse events not fired when mouse is over the inner object in silverlight

I have a rectangle in my XAML which acts as container of a textbox (textbox is displayed on the rectangle), I can move rectangle by dragging it (using mousedown and mousemove events of rectangle object), but there is a problem with textbox as it covers a big part of my rectangle, so when users click on mouse (and try to move it), there is a huge chance that they click actually on the textbox, so rectangle mouse events don't fire up and nothing works! (i.e. no move is possible). How can I overcome this problem? (I use SL 5 & VS 2012)
The TextBox control handles the mouse down events and stops them from bubbling up the visual tree. (if you're using a canvas though bubbling events won't help)
You can add your own event handler to the mouse events through code e.g.
txtbox.AddHandler(TextBox.MouseLeftButtonDownEvent,
new MouseButtonEventHandler(<your handler>), true);
where your handler has the signature:
private void <your handler>(object sender, MouseButtonEventArgs e)
{
}
Let me know if the particulars of your situation require something more than this.

Setting focus on WPF ComboBox not always working

I have a solution, that has two projects, the main one and a small shared control that's used as well. In our application, a certain feature opens this shared control in a new window. I want to set the focus to the first combobox in this control when the window opens.
In my code, on the window that loads the shared control, at the end of the _Loaded event, I set focus to this combobox. But when running the code - I still have to hit tab to have 'keyboard' focus on the box (as in, I would have to hit tab to then start typing the name of one of the items in the list).
If I set a breakpoint here, hit it, and then continue - it actually is set the way it should be. If I use a WPF inspector - IsFocused is also set.
Other things noticed:
If I hit tab (to get what I want), then tab back, it takes me to the last control on the form, not to this unknown control. This makes me believe the focus is set right, but for some reason doesn't have correct keyboard focus.
If I try to use MoveNext in code, it actually selects the next item in the window, outside of the control.
How do I properly set focus here? On another combobox in the 'main' project, just calling .Focus() worked correctly.
try to postpone the focus() after all events are handled and bindings updated with QueueUserWorkItem. Something like this :
public delegate void VoidDelegate();
private void Window_Loaded(object sender, RoutedEventArgs e)
{
// Some other things to do here.
System.Threading.ThreadPool.QueueUserWorkItem
(x => this.Dispatcher.Invoke(
new VoidDelegate(SetFocus), null));
}
private void SetFocus()
{
MyControlIWantToSetFocusOn.Focus();
}

How do I find out where the focus is going in my WPF application?

I have a search screen in my WPF application. The screen is implemented as a UserControl in a TabItem of a TabControl. When the user switches to the Search tab, I want the focus to go into one particular field.
So I added a Loaded event handler to the UserControl tag in the Xaml and I called the Focus method of the control I want to have the initial focus in the Loaded event handler. This worked great until I upgraded the Telerik control library I'm using today. Now, when I switch to the Search tab, the focus is NOT in the field I want to have it, but I can't tell what control does have the focus.
The field I want to have focus already has GotFocus & LostFocus event handlers for other reasons. I remembered that in Win Forms, the LostFocus event handler arguments tell you which control is going to get the focus. So I put a breakpoint in my LostFocus handler & discovered that the arguments to the LostFocus event handler in WPF don't include that information.
How can I figure out where the focus is going without putting GotFocus handlers on every control in my UserControl?
Tony
You can try putting your breakpoint on the LostKeyboardFocus Attached Event instead of the LostFocus Event. It uses the KeyboardFocusChangedEventArgs Class which does have properties that show which element had focus and where the focus is going.
private void textBox1_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
{
textBox1.Text = ((FrameworkElement)e.NewFocus).Name ;
}
Try to press Tab Key and see if it helps you find the control in focus.
You can also use Snoop as suggested in this Q/A: Any tips on debugging focus issues in WPF?
For starters, Snoop shows the current focused element and the current
FocusScope in the status bar.
You can get it to show you all the GotFocus and LostFocus events:
1. Run your app.
2. Run Snoop.
3. Choose your app in the dropdown.
4. Click the binoculars ("Snoop") button.
5. On the right pane, click the Events tab.
6. Click to bring down the dropdown.
7. Scroll down to the Keyboard section and check GotKeyboardFocus, LostKeyboardFocus, and optionally the PreviewXXX events.
8. Now do what you need to do to manipulate focus and watch the Snoop window.
Similarly you can track the FocusManager events the same way.

How to disable mousedown event on Silverlight listbox

I have a third-party image control inside an SL5 listbox itemtemplate. This makes for a nice scrollable gallery of images.
Now for the trouble: the third-party image control (LeadTools v17.5) has an interactive feature wherein mouseleftbuttondown causes a draggable magnifying glass to appear. This works great when the control is not hosted in a listbox. But clicking on the control within a listboxitem does nothing. After some research I "believe" this is because the listboxitem is trapping the mouseleftbuttondown event marking it as handled so the image control never sees it. In my application I have no need to handle the mouseleftbuttondown event at the listbox level (other buttons etc control my UI). Assuming I'm correct, is there a way to stop the listboxitem from listenting to this event?
Or perhaps I'm completely wrong about the cause. In that case any other ideas about why the listbox appears to block mouseleftbuttondown events from reaching the controls within is appreciated.
Thanks,
Mark
Instead of trying to keep the ListBoxItem from handling the event, you might be able to use UIElement.AddHandler with handledEventsToo: true, if you can get the necessary UIElement and Delegate references to trigger the image control's feature.
Thanks for your suggestions. In this case it turns out the quick solution was to add this handler to the image control:
private void leadGalleryImageViewer_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
base.OnMouseLeftButtonDown(e);
e.Handled = true;
}
From this I gather the mouseleftbuttondown event was being received by the image control all along, but likely as it bubbled up through the listitem and beyond, the listitem did its thing then marked it as handled effectively killing anything the image control was trying to do. By marking the event as handled at the image control level, the listitem ignores it.

Events of AutoCompleteBox in WP7

I'm using the AutoCompleteBox of the silverlight toolkit in my WP7 application.
When the box has focus, the SIP (soft input panel) popus up. Good.
Now, I would like to be able to detect :
when the user selects a value in the
AutoCompleteBox DropDown
when the user clicks on "enter" in
the SIP, that means he validates his
inputs
This should be 2 differents events as I have 2 differents things to do in both cases.
There's the SelectionChanged event, But I'm unable to make the difference...
Thanks in advance for any help.
Best regards
You can use .Focus() on another control on the page to close the SIP.
You can test for the Enter key in the OnKeyUp event for the TextBox and move the focus.
e.g.
private void myAutocompleteBox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e) {
if (e.Key == Key.Enter) {
addButton.Focus();
}
}
You can also test for an item selected by the AutoCompleteBox in the Textbox.SelectionChanged event and move the focus.
This post demonstrates doing the latter.
AutoCompleteBox in Windows Phone 7 « Roger Gullhaug's Blog

Resources