Is there a ProcessTabKey equivalent in Silverlight (focus on next control in hierarchy)
Is there an equivalent method to WinForm's ProcessTabKey in Silverlight, or a way of correctly simulating it?
Looking around what I see are people are hard coding every single control (text1 has a KeyDown event to focus text2, text2 has a KeyDown event to focus text3, etc). A few have progressed to querying part of the layout hierarchy based on TabIndex, but by default all controls have the same TabIndex so it is once again a manual setup task.
A final wrinkle is dealin开发者_Python百科g with nested containers. For example a vertically oriented stackpanel of textboxes, followed by a horizontally oriented stackpanel of buttons. Most programmatic approaches I've seen try to assume all controls are direct children of the same parent container.
I understand that Silverlight must operate under some restrictions, to prevent a Silverlight application from tabbing focus back to part of the browser (a potential security risk), but I hope there is some way to create a proper Enter-to-Tab setup without hand crafting all forms.
There is a related question in StackOverflow: Auto-tab in Silverlight 3
I'm using a custom attached Behavior (System.Windows.Interactivity) to avoid code behind because Iand works well with MVVM.
<UserControl x:Class="SCO.Ria.UI.Views.LoginView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:interactivity="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:Caliburn.Micro.Focus;assembly=Caliburn.Micro.Focus" mc:Ignorable="d"
d:DesignHeight="154" d:DesignWidth="468">
<interactivity:Interaction.Behaviors>
<local:TabNextBehavior/>
</interactivity:Interaction.Behaviors>
</UserControl>
TabNextBehavior.cs:
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Interactivity;
namespace Caliburn.Micro.Focus {
public class TabNextBehavior : Behavior<Control> {
protected override void OnAttached() {
base.OnAttached();
this.AssociatedObject.KeyUp += AssociatedObject_KeyUp;
}
protected override void OnDetaching() {
base.OnDetaching();
this.AssociatedObject.KeyUp -= AssociatedObject_KeyUp;
}
void AssociatedObject_KeyUp(object sender, KeyEventArgs args) {
if (args.Key == System.Windows.Input.Key.Enter) {
DependencyObject parent = null;
if (AssociatedObject is ChildWindow)
parent = ((ChildWindow)parent).Content as DependencyObject;
else parent = AssociatedObject;
parent.TabNext(); //extensin Method from VisualTreeExtensions.cs
}
}
}
You can see a gist here: https://gist.github.com/4576803
精彩评论