Events of AutoCompleteBox in WP7 - silverlight

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

Related

Winforms TextBox gets focus but doesn't capture keyboard input

This has me baffled.
I've written a popup box which consists of a WinForms UserControl hosted inside a WindowsFormsHost, which, in turn, is hosted in a Primitives.Popup which is displayed on the screen. The whole application is WPF, but this control was lifted from an earlier application written in WinForms.
The popup is activated by an external event (an incoming phone call from a CTI Server).
Inside the UserControl is a textbox control. When the user clicks in the text box, I call the Focus method on the Popup, then call the Focus method on the textbox. The textbox gets the focus. I can be fairly sure of that because the box shows a cursor after clicking in it, and also I have a "GotFocus" event handler that prints a debugging message.
However, if there was another program active at the time the incoming event occurs, any keys that are pressed on the keyboard continue to go to that program, not to the text box. Only if the user clicks in another part of my application (i.e., part of the screen outside the popup) to make it the active program, and then clicks in the text box is the text box able to receive keyboard input.
I hope I've given enough information without overwhelming you with the myriad of details. If there's something else anyone needs to point me in the right direction, I'll be happy to provide it.
Because the WinForm TextBox is hosted, setting focus on it does not activate the hosting WPF Window. Add a line to activate the Window.
private void TextBox_Click(object sender, EventArgs e)
{
this.Activate(); //activate the Window
(sender as System.Windows.Forms.TextBox).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 can I redirect the focus in WPF?

How can I set an element to be the default focus of other elements?
For example, say I have the following:
<StackPanel><Label/><Button/></StackPanel>
Clicking on any element will give it the focus if the Focusable is true.
However, what I need to say is "if the user clicks anywhere in the stackpanel, the button should get the focus". In other words, clicking on the label will give the focus to the button. This should work in small samples such as this one but also much larger ones with control templates.
Is this possible?
What about something like:
private void StackPanel_GotFocus(object sender, RoutedEventArgs e)
{
<elementName>.Focus();
}
StackPanel has GotFocus event. In the event handler for that event, give button the focus ( by calling button1.Focus()).

Detecting loss of keyboard focus in a Silverlight application

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();
}
}

How do I get the KeyDown event to fire in a custom container control?

I have a custom container control (deriving from FlowLayoutPanel) which contains zero or more child controls dragged there by the user. When a child control is clicked, it is "selected." (It is drawn with a colored border and options are available for altering its properties.)
I would like to handle the Delete key so that, if the user is currently working in the container control (clicked within the control or on a child control, for instance) the currently selected control (if there is one) is deleted.
I already have the delete functionality working using a right-click context menu on the child items. My problem is handling the Delete key. I cannot figure out how to get the KeyDown event to raise within my container control. I know it has something to do with focus, so that Control.Select() (or its equivalent) must be called, but what is the best way to go about this? Where does this focus logic reside? Or is there a better way?
I do not want to handle the KeyDown event in the form and then sniff out where the focus is. This is a reuseable container control and I want the logic to reside there.
What do I have to do to get the KeyDown event to fire on a custom control?
public class MyContainer : FlowLayoutPanel
{
protected override void OnKeyDown(KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{
MessageBox.Show("How do I get here?");
e.Handled = true;
}
base.OnKeyDown(e);
}
}
The KeyDown event is listed as unmeaningful for the FlowLayoutPanel control on MSDN. Suggest the PreviewKeyDown event as an alternative.
Is it possible that the items dragged into the container are receiving the event?
Perhaps after an item is drug into your container, you need to manually set the focus to the container.

Resources