Hotkeys in Silverlight? - silverlight

Is there a way to create global keys in Silverlight? I added a KeyDown event handler to the topmost Page element. I can catch the key signal when elements like a Button, TextBox, Calendar, and RichTextBox have focus. But the DatePicker won't let me handle the key down event at the Page level. Any way to make a hotkey in non-OOB SL?
P.S. I am assuming that the DatePicker control behaves differently than others as it is still in the Silverlight Toolkit.

have you tried using the AddHandler method? With this method you can add a handler and define if you want to handle already handled events, too.
In my simple example I added it to the RootVisual in the app.xaml.cs.
private void Application_Startup(object sender, StartupEventArgs e)
{
this.RootVisual = new MainPage();
RootVisual.AddHandler(UIElement.KeyDownEvent, new KeyEventHandler(HandleKeyDown), true);
}
private void HandleKeyDown(object sender, KeyEventArgs e)
{
//Add your keyhandling code here
Debug.WriteLine(e.Key);
}
I tried it with a DatePicker and it works.
Hope this helps!
BR,
TJ

This may help you. The author provides two ways using HTML bridge (when your Silverlight does not have focus) and using the KeyUp event on Application.RootVisual.
I have not tried it myself so I wouldn't know if it's working.

Related

How can I return focus to a control in WPF from within the lost focus event of the same control?

In my WPF application I am doing some input validation in the lostfocus event of a textbox control. If the text does not meet the correct criteria I clear the text in the textbox. That works fine. What I am having trouble doing is returning focus to the control after viewing a message box. For example when I use the below code, the text box focus method re-triggers the lost focus event after I close the message box.
private void TaskNameBox_LostFocus(object sender, RoutedEventArgs e)
{
... validation logic here
MessageBox.Show("Message.", "Error", MessageBoxButton.OK);
TaskNameBox.Focus();
}
I don't know why the Focus method would retrigger the lost focus event, but I need a way to get the focus back on the TaskNameBox Control after losing it. Any suggestions would be very much appreciated. I am new to WPF.
Use it following:
private void TaskNameBox_LostFocus(object sender, RoutedEventArgs e)
{
... validation logic here
MessageBox.Show("Message.", "Error", MessageBoxButton.OK);
Dispatcher.BeginInvoke((ThreadStart)delegate
{
TaskNameBox.Focus();
});
}
try this one
FocusManager.SetFocusedElement
https://msdn.microsoft.com/en-us/library/system.windows.input.focusmanager.setfocusedelement(v=vs.110).aspx
FocusManager.SetFocusedElement(parentElement, txtCompanyID)

Transfer routed event from panorama to tile

I have a app in Metro Style, and in it one page contain the "Panorama" that contain multiple tiles.
In panorama.cs
the event
protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
{}
and
protected override void OnPreviewMouseMove(MouseEventArgs e)
{}
are used to scroll the panorama from left to right and vice versa.
On each "Tile" I have defined a function
private void Tile_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
{}
this is used to open another form.
but due to panorama events, this method is not called and if I comment the above events of "Panorama.cs" I am unable to scroll left to right and vice-versa. How to achieve both.
Thanks in advance
After some RnD and hit and trial testing I found this can be done simply.
Instead of using "MouseDoubleClick" event handler , just use "PreviewMouseDoubleClick".
:)

Silverlight SDK DataGrid not always fire MouseLeftButtonUp event?

Why does the DataGrid not always fire the MouseLeftButtonUp event?
I try to implement a single click behaviour on DataGridTextColumn and bind to this event.
dataGrid.MouseLeftButtonUp += OnDataGridMouseLeftButtonUp;
In the handler I call BeginEdit() and set focus on the TextBox element. It works when I get the event, but it is not always fired? Does anyone know how to fix that?
Thanks!
Try using AddHandler instead.
dataGrid.AddHandler(UIElement.MouseLeftButtonUpEvent,
new MouseButtonEventHandler(OnMouseLeftButtonUp), true)
...
private void OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
// etc...
}

How To Navigate From Child Window To User Control Silverlight?

I have a child window with two buttons. When a button click event occurs I need to navigate to a specific user control. How to do this in Prism Framework?
If you are using PRISM will be simple as fires a notification event using the event aggregator. Anyway if not, this will solve your problem.
You need when you fire the childwindow to add the close event to that childwindow, like this:
YourChildWindow childWindow = new YourChildWindow ();
childWindow.Closed += new EventHandler
(YourChildWindowClosed);
childWindow.Show();
Then,
?
void YourChildWindowClosed(object sender, EventArgs e)
{
YourChildWindow yourChildWindow = ((YourChildWindow)sender);
if
(yourChildWindow.DialogResult.Value)
{
... do your things here ...
}
}
I hope this can help you.

Is there a way to watch WPF Routed Events?

I was wondering if there's a way to watch all RoutedEvents that are raised in a WPF application. A way to write some info about the events fired to the console would be prefect to see what's going on.
I've found another way:
I've added this to the loaded handler of my UserControl.
var events = EventManager.GetRoutedEvents();
foreach (var routedEvent in events)
{
EventManager.RegisterClassHandler(typeof(myUserControl),
routedEvent,
new RoutedEventHandler(handler));
}
and this is the handler method:
internal static void handler(object sender, RoutedEventArgs e)
{
if (e.RoutedEvent.ToString() != "CommandManager.PreviewCanExecute" &&
e.RoutedEvent.ToString() != "CommandManager.CanExecute")
Console.WriteLine(e.OriginalSource+"=>"+e.RoutedEvent);
}
The CanExecute events are a bit too much in my case. If you would like to see these too, just remove the if statement.
Yes, but it requires some reflection. You're better off using a tool like Snoop that already does the hard lifting for you.
In the tab Events you can see list of events, and the element that handled it.

Resources