我正在寻找一种自动设置Silverlight UserControl初始焦点到特定控件的方法。我有一个登录页面,有一个用户名文本框,我希望用户在访问该页面时光标已经定位并等待在用户名文本框中,而不是让他们点击该框。
我尝试在UserControl的Loaded事件中调用.Focus,但没有成功。有人知道怎么做吗?
我尝试在UserControl的Loaded事件中调用.Focus,但没有成功。有人知道怎么做吗?
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
HtmlPage.Plugin.Focus();
MyTextBox.Focus();
}
<%@ Page Language="C#" AutoEventWireup="true" %>
<%@ Register Assembly="System.Web.Silverlight" Namespace="System.Web.UI.SilverlightControls"
TagPrefix="asp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" style="height:100%;">
<head runat="server">
<title>Test Page For TextFocusTest</title>
<script type="text/javascript">
window.onload = function()
{
document.getElementById('Xaml1').focus();
}
</script>
</head>
<body style="height:100%;margin:0;">
<form id="form1" runat="server" style="height:100%;">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<div style="height:100%;">
<asp:Silverlight ID="Xaml1" runat="server" Source="~/ClientBin/TextFocusTest.xap" Version="2.0" Width="100%" Height="100%" />
</div>
</form>
</body>
</html>
一旦您的SL控件获得焦点,您可以使用类似以下内容将焦点进一步设置到特定控件:
namespace SilverlightApplication2
{
public partial class MainPage : UserControl
{
public MainPage ()
{
InitializeComponent ();
this.GotFocus += new RoutedEventHandler (MainPage_GotFocus);
}
void MainPage_GotFocus (object sender, RoutedEventArgs e)
{
uxLogin.Focus ();
}
}
}
其中uxLogin在XAML中定义为:
<TextBox x:Name="uxLogin" Height="25" Width="100" />
如果您想以PRISM或MVVM的方式进行操作(摆脱代码后台),可以实现一种行为。在我的情况下,如果用户名为空,则将焦点放在用户名字段上,如果密码已设置,则将焦点放在密码上(因此有2个参数)。
我的实现:
public static class ControlTextFocusBehavior
{
public static readonly DependencyProperty FocusParameterProperty = DependencyProperty.RegisterAttached(
"FocusParameter",
typeof(string),
typeof(ControlTextFocusBehavior),
new PropertyMetadata(OnSetFocusParameterCallBack));
public static readonly DependencyProperty IsEmptyFocusedProperty = DependencyProperty.RegisterAttached(
"IsEmptyFocused",
typeof(bool),
typeof(ControlTextFocusBehavior),
new PropertyMetadata(true));
private static void OnSetFocusParameterCallBack(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
{
Control control = dependencyObject as Control;
if (control != null)
{
control.Loaded += new RoutedEventHandler(control_Loaded);
}
}
private static void control_Loaded(object sender, RoutedEventArgs e)
{
Control control = sender as Control;
control.Loaded -= new RoutedEventHandler(control_Loaded);
DependencyObject dependencyObject = sender as DependencyObject;
if (dependencyObject != null)
{
bool isEmptyFocused = GetIsEmptyFocused(dependencyObject);
bool isNullOrEmpty = string.IsNullOrEmpty(GetFocusParameter(dependencyObject));
if ((isEmptyFocused && isNullOrEmpty) ||
(!isEmptyFocused && !isNullOrEmpty))
{
HtmlPage.Plugin.Focus();
control.Focus();
}
}
}
public static void SetFocusParameter(DependencyObject dependencyObject, string parameter)
{
dependencyObject.SetValue(FocusParameterProperty, parameter);
}
public static string GetFocusParameter(DependencyObject dependencyObject)
{
return dependencyObject.GetValue(FocusParameterProperty) as string;
}
public static void SetIsEmptyFocused(DependencyObject dependencyObject, bool parameter)
{
dependencyObject.SetValue(IsEmptyFocusedProperty, parameter);
}
public static bool GetIsEmptyFocused(DependencyObject dependencyObject)
{
return (bool)dependencyObject.GetValue(IsEmptyFocusedProperty);
}
}
window.onload = function()
{ document.getElementById('Xaml1').focus(); }
为了在所有浏览器中正常工作,您必须为id="Xaml1"的元素设置tabIndex。
抱歉我的英语不好 :)